2013-04-04 152 views
2

我仍然在iOS開發有點綠,不斷得到那我不知道有關的警告。不兼容的指針類型的警告

我使用一個tableview中自定義單元格,並設置其類是的UITableViewCell的子類,稱爲SiteCell。在我的「didSelectRowAtIndexPath方法」的方法,當我聲明所選擇的小區作爲SiteCell類型,收到以下警告:

不相容指針類型初始化SiteCell與類型的 表達__strong的UITableViewCell

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{ 
    SiteCell *cell = [tableView cellForRowAtIndexPath:indexPath]; 

    NSString *rowIdentity = [NSString stringWithFormat:@"%d", indexPath.row]; 
    [selectedRows setObject:[cell.siteTitleLabel text] forKey:rowIdentity]; 

    if(cell.accessoryType == UITableViewCellAccessoryCheckmark){ 
     cell.accessoryType = UITableViewCellAccessoryNone; 
    }else{ 
     cell.accessoryType = UITableViewCellAccessoryCheckmark; 
    } 
} 

任何人都可以闡明如何擺脫這一警告的任何輕?

回答

15

假設的cellForRowAtIndexPath正確編碼,被稱爲總是返回SiteCell,你只需要簡單地轉換爲SiteCell*

SiteCell *cell = (SiteCell*)[tableView cellForRowAtIndexPath:indexPath]; 
3

你的代碼應該是這樣的,因爲你必須將UITableViewCell投射到您的子類SiteCell

SiteCell *cell = (SiteCell *)[tableView cellForRowAtIndexPath:indexPath]; 
+0

當然!謝謝 – Pheepster 2013-04-04 18:26:06

1

正確的做法。你需要NSIndexPath

SiteCell *cell = (SiteCell *) [self.tableView cellForRowAtIndexPath:[NSIndexPath indexPathForRow:row inSection:section]]; 
相關問題