I'm using a Custom cell in my UITableView, where I receive values from objects what I add to this cell like this:

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

All working good but I have one warning (this is only 1 warning in all my app) and I want to remove this warning:

 incompatible pointer types initializing StuardReportCustomCell with an expression of type 
 UITableviewCell 

StuardReportCustomCell .h file

#import <UIKit/UIKit.h>

@interface StuardReportCustomCell : UITableViewCell

@end

This is 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;
}

Solution:

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

or

NSIndexPath *indexPath = [self.ReportTableView indexPathForSelectedRow];
NSString *text = [lbTime objectAtIndex:indexPath.row];
有帮助吗?

解决方案

You can get rid of the warning by adding a cast. However, you should not be reading from the text of the cell, because this mixes up the levels of the Model-View-Controller: you should get the value from the model instead.

Therefore, instead of writing, say,

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

you should write

NSIndexPath *indexPath = [self.ReportTableView indexPathForSelectedRow];
NSString *text = [lbTime objectAtIndex:indexPath.row];
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top