2009-10-16 49 views
2

我需要使用複選框單元格的幫助。我目前將該對象添加到tableview。它看起來不錯,直到我試圖建立和運行程序,我無法檢查複選框。我目前使用的tableview顯示項目運行時每個項目的複選框,所以我可以有多個選擇。表格視圖中的複選框單元格:用戶無法檢查它

我是新來的xcode和我一直卡住一個星期這個問題。我試過谷歌,但仍然沒有運氣。

任何片段,答案或解釋非常感謝。

回答

5

首先我們需要編輯這個方法:- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath。假設您生成了一個基於導航的應用程序,該方法應該已經存在,只有註釋掉。我不知道你實現的確切細節,但你必須跟蹤tableView中每個單元格的複選框狀態。舉例來說,如果你有一個BOOL數組,下面的代碼將工作:

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

if (checkboxArray[indexPath.row]) 
    checkboxArray[indexPath.row] = NO; 
else 
    checkboxArray[indexPath.row] = YES; 

[self.tableView reloadData]; 
} 

現在我們知道了細胞需要有一個對號旁邊,下一步是要修改單元格的顯示方式。 - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath處理每個單元格的繪圖。建立關前面的例子,這是你將如何顯示的複選框:

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

    static NSString *CellIdentifier = @"Cell"; 

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 
    if (cell == nil) { 
     cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease]; 
    } 

if (checkboxArray[indexPath.row]) { 
    cell.accessoryType = UITableViewCellAccessoryCheckmark; 
} 
else 
    cell.accessoryType = UITableViewCellAccessoryNone; 

// Configure the cell. 

    return cell; 
} 

如果我們不叫reloadData,複選標記不會顯示出來,直到它熄滅屏幕和重新出現。由於單元格被重用的方式,每次需要明確設置accessoryType。如果僅在選中單元格時設置樣式,則可能不必檢查的其他單元格在滾動時會出現複選標記。希望這給你一個關於如何使用複選標記的總體思路。

相關問題