2013-06-04 32 views
0

我是IOS6 dev的新手。我遇到了UITableView的問題。我的代碼如下,在所選行的末尾顯示覆選標記。但我收到一個錯誤,如「@interfaceUITableView聲明選擇器cellForRowAtIndexPath:「。 UITableViewcellForRowAtIndexPath的方法,但tableView不能用 顯示它。我不知道爲什麼。請幫忙。不可見@interface for「UITableView」聲明選擇器「cellForRowAtIndexPath:」

下面是代碼:

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{ 

    UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath]; -----error line 
    cell.accessoryType = UITableViewCellAccessoryCheckmark; 
    } 

的問題是 「的tableView」 不能識別所有的UITableView下的方法。有些知道如「numberOfRowsInSection」。我無法弄清楚爲什麼。

+0

indexPath.row代替錯誤行中的indexPath。 – lakesh

+0

你是如何創建'UITableView'的實例的? – Zen

+3

@lakesh這是一個錯誤 – Anupdas

回答

0

TableView本身並沒有實現這個選擇器。 此方法的充分選擇是

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 

並且是從協議爲代表。你的委託(例如viewController)必須實現這個方法。不建議(也不可能)從桌子上取出細胞對象。

相反,更改基礎數據,並與

[tableView reloadData]; 
+1

這裏是它應該http://developer.apple.com/library/ios/#documentation/uikit/reference/UITableView_Class/Reference/Reference.html – Zerho

+0

你是對的。沒關係。 – Herm

+0

+1,因爲這解決了一個微妙的問題,那就是你肯定應該改變底層模型,並且'tableview:cellForRowAtIndexPath:'應該根據該模型中的數據設置複選標記。很重要。但是,-1因爲我不同意重裝整個桌子。糟糕的UX。至多,'reloadRowsAtIndexPaths'。但是,關鍵問題在於OP的代碼很好。他的直接問題不在於這種方法。它休息在別處。 – Rob

0

你的問題不是你包括代碼示例中重繪你的表。你的問題在別處。我們無法根據此片段診斷問題。您將不得不與我們共享更完整的代碼示例。


與您的問題無關,您的didSelectRowAtIndexPath中存在一個微妙的問題。你不應該在這裏更新cellAccessoryType。你真的應該更新你的模型,支持你的用戶界面。如果表中的行數多於在任何給定時刻可見的數量,這將是至關重要的。

爲了說明這個想法,讓我們假設你的模型是一個具有兩個屬性的對象數組,title的單元格以及單元格是否爲selected

因此,您cellForRowAtIndexPath可能看起來像:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    static NSString *cellIdentifier = @"Cell"; 
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier]; 

    RowData *rowObject = self.objects[indexPath.row]; 
    cell.textLabel.text = rowObject.title; 

    if (rowObject.isSelected) 
     cell.accessoryType = UITableViewCellAccessoryCheckmark; 
    else 
     cell.accessoryType = UITableViewCellAccessoryNone; 

    return cell; 
} 

和你didSelectRowAtIndexPath可能看起來像:

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    RowData *rowObject = self.objects[indexPath.row]; 
    rowObject.selected = !rowObject.isSelected; 

    [tableView reloadRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade]; 
} 

同樣,你的編譯器警告/錯誤,無疑是從源的一些其他問題,莖代碼,因爲您的原始代碼段在語法上是正確的。我只是想改正你的didSelectRowAtIndexPath中的一個不同的缺陷。在MVC編程中,您確實需要確保更新模型(然後更新視圖),而不僅僅是更新視圖。

但是,要清楚的是,如果您不糾正導致當前編譯器警告/錯誤的錯誤,則無論您將哪些內容放入didSelectRowAtIndexPath中,您可能會收到另一個警告。你必須確定爲什麼編譯器在你當前的代碼中不起作用。

+0

謝謝你們。通過將xcode從4.6.2更新到4.6.3解決了問題。這很奇怪,我不知道它爲什麼起作用。奇怪。 –

相關問題