警告 - 指针类型不兼容,如何删除?

时间:2014-04-17 18:23:04

标签: ios objective-c pointers uitableview xcode5.1

我正在我的UITableView中使用自定义单元格,我在这里接收来自对象的值,我将这样添加到此单元格中:

    NSIndexPath *indexPath = [self.ReportTableView indexPathForSelectedRow];
    StuardReportCustomCell *cell = [_ReportTableView cellForRowAtIndexPath:indexPath];
    NSString *text = cell.MyLabelInCell.text;

一切正常,但我有一个警告(这在我的所有应用中只有1个警告),我想删除此警告:

 incompatible pointer types initializing StuardReportCustomCell with an expression of type 
 UITableviewCell 

StuardReportCustomCell .h文件

#import <UIKit/UIKit.h>

@interface StuardReportCustomCell : UITableViewCell

@end

这是cellForRowAtIndexPath

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath   *)indexPath
{
static NSString *CellIdentifier = @"Cell";

StuardReportCustomCell *Cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];

if(!Cell){
    Cell = [[StuardReportCustomCell alloc] initWithStyle:UITableViewCellStyleDefault  reuseIdentifier:CellIdentifier];
}

Cell.lbNum.text = [lbNum objectAtIndex:indexPath.row];
Cell.lbRouteNum.text = [lbRouteNum objectAtIndex:indexPath.row];
Cell.lbTime.text = [lbTime objectAtIndex:indexPath.row];
Cell.lbCity.text = [lbCity objectAtIndex:indexPath.row];
Cell.lbCameIn.text = [lbCameIn objectAtIndex:indexPath.row];
Cell.lbIn.text = [lbIn objectAtIndex:indexPath.row];
Cell.lbOut.text = [lbOut objectAtIndex:indexPath.row];
Cell.lbCameOut.text = [lbCameOut objectAtIndex:indexPath.row];

return Cell;
}

解决方案:

StuardReportCustomCell *cell = (StuardReportCustomCell  *)[_ReportTableView      cellForRowAtIndexPath:indexPath]; 

NSIndexPath *indexPath = [self.ReportTableView indexPathForSelectedRow];
NSString *text = [lbTime objectAtIndex:indexPath.row];

1 个答案:

答案 0 :(得分:1)

您可以通过添加演员来摆脱警告。但是,您不应该从单元格的文本中读取,因为这会混淆模型 - 视图 - 控制器的级别:您应该从模型中获取值。

因此,而不是写作,

NSIndexPath *indexPath = [self.ReportTableView indexPathForSelectedRow];
StuardReportCustomCell *cell = (StuardReportCustomCell*)[_ReportTableView cellForRowAtIndexPath:indexPath];
NSString *text = cell.lbTime.text;

你应该写

NSIndexPath *indexPath = [self.ReportTableView indexPathForSelectedRow];
NSString *text = [lbTime objectAtIndex:indexPath.row];