我在嘗試實現複選框作爲待辦事項列表的一部分時遇到了一些麻煩。 我有一個UITableView,每個UITableViewCell都應該有自己的複選框,在左邊。如何在使用UITableViewCell緩存時將複選框添加到左側的UITableView?
如果有人能告訴我如何實現這一點,我將不勝感激。我已經廣泛地研究了這個網站上的問題,但仍然無法弄清楚如何實現這一點。
滾動tableview時會出現問題。例如,如果我有20個待辦事項,當我滾動時,將被選擇的複選框被取消選擇,並且將被取消選擇的複選框以看起來隨機的方式被選中。它必須是滾動的東西。
當系統重新使用單元時,究竟是什麼被緩存和發生?我是否抓住了一個空單元格,只是引用了裏面的空子視圖?
如果tablecell#1具有標籤爲10的視圖,tablecell#2是否能夠具有標籤爲10的視圖?或者是標籤不能在整個tableView中重複使用? (我甚至不確定這是否是問題)
謝謝!
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
switch (selectedSegment) {
case Todos:{
static NSString *ToDoCellIdentifier = @"TodoTableViewCell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifierToDoCellIdentifier];
UITextView *taskTextView = nil;
UIButton *checkBox = nil;
UIImageView *highPriorityIcon = nil;
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:signoutTableViewCellIdentifier] autorelease];
checkBox = [[UIButton alloc]initWithFrame:CGRectMake(1, 1, 25, 25)];
checkBox.tag = indexPath.row;
UIImage *normalImage = [UIImage imageNamed: @"[email protected]"];
UIImage *selectedImage = [UIImage imageNamed: @"[email protected]"];
[checkBox setImage:normalImage forState:UIControlStateNormal];
[checkBox setImage:selectedImage forState:UIControlStateSelected];
[checkBox addTarget:self action:@selector(historyButtonPressed:) forControlEvents:UIControlEventTouchUpInside];
[cell.contentView addSubview:checkBox];
[checkBox release];
//cell.contentView.backgroundColor = [UIColor lightGrayColor];
cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
NSLog(@"creating a new cell");
}
if (!checkBox) {
checkBox = (UIButton*)[cell viewWithTag:indexPath.row];
}
}
if ([[self.tasksCheckedStates objectAtIndex:indexPath.row]boolValue]) {
checkBox.selected = YES;
}
else {
checkBox.selected = NO;
}
return cell;
break;
}