2015-01-04 61 views
0

我有這種相對簡單的helper方法:隱式轉換錯誤NSIndexPath

- (float)imageHeightForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    CGRect screenRect = [[UIScreen mainScreen] bounds]; 
    CGFloat screenWidth = screenRect.size.width; 
    float imageWidth = [[self.widthArray objectAtIndex:indexPath.row] floatValue]; 
    float ratio = screenWidth/imageWidth; 
    float imageHeight = ratio * [[self.heightArray objectAtIndex:indexPath.row] floatValue]; 
    return imageHeight; 
} 

的另一種方法,但這種方法調用的時候,其工作完全正常:

- (UIImage *)imageWithImage:(UIImage *)image forRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    CGRect screenRect = [[UIScreen mainScreen] bounds]; 
    CGFloat newWidth = screenRect.size.width; 
    CGFloat newHeight = [self imageHeightForRowAtIndexPath:indexPath.row]; 
    UIGraphicsBeginImageContext(CGSizeMake(newWidth, newHeight)); 
    [image drawInRect:CGRectMake(0, 0, newWidth, newHeight)]; 
    UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext(); 
    UIGraphicsEndImageContext(); 
    return newImage; 
} 

編譯器說: Implicit conversion from 'NSInteger' to 'NSIndexPath' is disallowed

我不知道爲什麼以及如何解決這個問題。任何想法?

+0

驚人的,它不識別錯誤的行! – 2015-01-04 14:11:55

+0

但很可能是這一行:'CGFloat newHeight = [self imageHeightForRowAtIndexPath:indexPath.row];'(Chuck'.row') – 2015-01-04 14:12:52

回答

1

你有2種方法:

  1. - (float)imageHeightForRowAtIndexPath:(NSIndexPath *)indexPath
  2. - (UIImage *)imageWithImage:(UIImage *)image forRowAtIndexPath:(NSIndexPath *)indexPath

每一個有NSIndexPath *參數。

現在,imageWithImage:forRowAtIndexPath:您呼叫imageHeightForRowAtIndexPath:

[self imageHeightForRowAtIndexPath:indexPath.row] 

,並在該行正在從indexPath(一NSInteger)服用row,並試圖將它傳遞給imageHeightForRowAtIndexPath::,這是預期的NSIndexPath *

這是錯誤的原因,因爲編譯器知道期望NSIndexPath *,它知道你提供的是NSInteger

來解決,該代碼更改爲:

CGFloat newHeight = [self imageHeightForRowAtIndexPath:indexPath];