2011-11-29 150 views
0

我正在開發一個具有UITableView和定製UITableViewCells的應用程序。單元格包含複選框和3個標籤。用戶可以通過點擊複選框來選擇多行。當我選擇多行並滾動表格視圖時,選中標記從複選框中消失。這意味着所選行被更改爲未選模式。這可以如何解決?在UITableView中滾動定製單元格

+0

請發表您的代碼爲tableView:cellForRowAtIndexPath: – Denis

回答

1

我認爲您的問題可能與單元重用有關。創建你的控制器的NSMutableDictionary(應該是和實例變量),並且像這樣創建:

dictionary = [[NSMutableDictionary alloc] initWithObjectsAndKeys: 
       [NSNumber numberWithBool:NO], [NSIndexPath indexPathForRow:0 inSection:0], 
       [NSNumber numberWithBool:NO], [NSIndexPath indexPathForRow:1 inSection:0], nil]; 

這樣做對所有的行,或者如果需要的話,計算行及其索引,但設置它們的所有對象到[NSNumber numberWithBool:NO

現在,tableView:didSelectRowAtIndexPath:

if ([(NSNumber *)[dictionary objectForKey:indexPath] isEqualToNumber:[NSNumber numberWithBool:NO]) { 
    [dictionary setObject:[NSNumber numberWithBool:YES] forKey:indexPath]; 
    // Do any visual updating necessary 
} else { 
    [dictionary setObject:[NSNumber numberWithBool:NO] forKey:indexPath]; 
    // Do any visual updating necessary 
} 

現在,我們有存儲單元是否被選中與否的一種方式,改變你的小區標識,爲您的電池工作:

NSString *identifier = [[dictionary objectForKey:indexPath] stringValue]; 
UITableViewCell *cell = [self.tableView dequeueReusableCellWithIdentifier:identifier]; 
if (cell == nil) { 
    cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:identifier] autorelease]; 
} 

// Do any visual updating necessary 
return cell; 

現在,更新您的自定義單元類以接受@"0"@"1"作爲標識符,並根據此輸入自動更新其選定狀態。

0

我猜測這是因爲細胞正在被重新使用。這意味着cellForRowAtIndex:在單元格回滾到視圖時被調用。複選框默認爲未選模式。將複選框的狀態存儲在一個數組中,並將單元格的複選框狀態設置爲

相關問題