我需要允許用戶爲某些項目選擇多個選項。爲此我使用以下內容。它需要兩個方法來取消UITableView中的accessoryType
唯一的問題是,我有選擇行兩次刪除accessoryView。我可以選擇第一個選擇的行。然而,選擇行後,如果觸摸它來取消選擇,首先只會突出顯示顏色,然後我必須再次單擊以取消選擇該行。
任何想法爲什麼會發生這種情況?我想在首次觸摸時取消選擇該行。
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
int i=indexPath.row;
if(self.multi == 1){
UITableViewCell *selectedCell = [tableView cellForRowAtIndexPath:indexPath];
NSLog(@"accessoryType:%d",selectedCell.accessoryType);
if (selectedCell.accessoryType == UITableViewCellAccessoryNone) {
selectedCell.accessoryType = UITableViewCellAccessoryCheckmark;
NSString *cellText = selectedCell.textLabel.text;
[self.selectedValues replaceObjectAtIndex:i withObject:cellText];
}else{
selectedCell.accessoryType = UITableViewCellAccessoryNone;
[self.selectedValues replaceObjectAtIndex:i withObject:@""];
}
}
else{
NSLog(@"not for multi select",nil);
UITableViewCell *selectedCell = [tableView cellForRowAtIndexPath:indexPath];
NSString *cellText = selectedCell.textLabel.text;
[self.delegate addItemViewController:self setPeculiarity:cellText setIndex:self.index];
[self dismissModalViewControllerAnimated:YES];
}
}
編輯:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:nil];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
cell.textLabel.text = [pecs objectAtIndex:indexPath.row];
if(self.selectedValues.count > 0){
for(int k = 0 ; k < [selectedValues count];k++){
if ([[self.selectedValues objectAtIndex:k] isEqualToString:[pecs objectAtIndex:indexPath.row]]) {
cell.accessoryType = UITableViewCellAccessoryCheckmark;
NSLog(@"pecs:%@",[pecs objectAtIndex:indexPath.row]);
}
}
}
// for background color
UIView *bgColorView = [[UIView alloc] init];
bgColorView.backgroundColor = [UIColor colorWithRed:0.2823 green:0.4509 blue:0.8588 alpha:1.0];
[cell setSelectedBackgroundView:bgColorView];
cell.textLabel.highlightedTextColor = [UIColor whiteColor];
return cell;
}
這是一個好的技巧取代
UITableViewRowAnimationNone
。但這種解決方案正在發生一件奇怪的事情。每當我選擇任何一行時,上面的分隔線對於當前單元格變得不可見。任何猜測爲什麼? – astuter添加您的'cellForRowAtIndexPath' .. –
仍然具有相同的奇怪效果。 – astuter