2014-04-17 61 views
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]; 
+2

只需鍵入投'StuardReportCustomCell *電池=(StuardReportCustomCell *)[_ ReportTableView的cellForRowAtIndexPath:indexPath]' –

+0

@ MidhunMP這將修復警告,但它會隱藏真正的問題。 – dasblinkenlight

+0

@dasblinkenlight在OP的問題中,他表示他在使用單元重用? – CrimsonChris

回答

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]; 
+0

謝謝你的工作。 –

相關問題