Uitableview附件类型复选标记重置代码

时间:2012-08-18 05:42:56

标签: iphone ios uitableview uibutton

我知道如何处理cellaccesory类型复选标记,在这里我需要一个按钮取消选中已勾选的整个表格单元格?在这里我的代码,任何人都可以帮我编码

- (void)viewDidAppear:(BOOL)animated
{
UIBarButtonItem *rest = [[UIBarButtonItem alloc]initWithTitle: @"RESET" style:      UIBarButtonItemStyleBordered target: self action: @selector(uncheckCells)];
UIBarButtonItem *flexibleSpace = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil];
[toolbar setItems:[NSArray arrayWithObjects:rest,flexibleSpace,flexibleSpace,home,nil]];
[self.navigationController.view addSubview:toolbar];
[self.tableView reloadData];
}

这是我的功能代码

-(void)uncheckCells//unchecking function
{
[self.tableView reloadData];//HERE WHAT SHOULD I DO
}
-(void)hom
{
[self dismissModalViewControllerAnimated:YES];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{ 
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier];
 }
cell.textLabel.text = [ar objectAtIndex:indexPath.row];    
cell.selectionStyle=UITableViewCellSelectionStyleNone;
if(cell.accessoryType==UITableViewCellAccessoryCheckmark)
{
cell.backgroundColor=UIColorFromRGB(0xd05818);
cell.detailTextLabel.text=@"packed";
cell.detailTextLabel.textColor=[UIColor cyanColor];
}
else
{
cell.backgroundColor=[UIColor whiteColor];
cell.detailTextLabel.text=@""; 
}
return cell;
[self.tableView reloadData]; 
}
(void) deselect
{   
[self.tableView deselectRowAtIndexPath:[self.tableView indexPathForSelectedRow]      animated:YES];

}
(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath     *)indexPath
{
[tableView reloadData];    
[tableView deselectRowAtIndexPath:indexPath animated:NO];
printf("User selected row %d\n", [indexPath row] + 1);
if ([[tableView cellForRowAtIndexPath:indexPath] accessoryType] ==      UITableViewCellAccessoryCheckmark)
{
[[tableView cellForRowAtIndexPath:indexPath]setAccessoryType:UITableViewCellAccessoryNone];
}
else
    [[tableView cellForRowAtIndexPath:indexPath]setAccessoryType:UITableViewCellAccessoryCheckmark];
    [self performSelector:@selector(deselect) withObject:nil afterDelay:0.0f];
[tableView reloadData];
}

2 个答案:

答案 0 :(得分:0)

如果您正在使用didSelectRowAtIndexPath:方法进行复选标记。 reloadData对您有用。

例如。

假设您有一个带有选择器uncheckCells

的按钮
-(void)uncheckCells
{
   [self.tableView reloadData];
}

这对你有用。

编辑:在cell.accessoryType = UITableViewCellAccessoryNone;条件中添加if (cell == nil)

或者这样做:

-(void)uncheckCells
{
for (int section = 0, sectionCount = self.tableView.numberOfSections; section < sectionCount; ++section) {
        for (int row = 0, rowCount = [self.tableView numberOfRowsInSection:section]; row < rowCount; ++row) {
            UITableViewCell *cell = [self.tableView cellForRowAtIndexPath:[NSIndexPath indexPathForRow:row inSection:section]];
            cell.accessoryType = UITableViewCellAccessoryNone;
            cell.accessoryView = nil;
        }
    }
}

我希望它能奏效。

答案 1 :(得分:-1)

通过阅读您的代码,您似乎在表视图本身上设置了复选标记。

您应该做的是存储数据库中是否有复选标记,并使用tableView:cellForRowAtIndexPath:方法中的此信息显示复选标记。

您的取消选择方法应该通过您的数据源并取消设置您在那里的任何标记,说明您的tableview是否有复选标记。

这样,当您滚动一个tableview,或者只是调用[tableView reloadData];时,您的数据存储将用于决定是否显示复选标记。

相关问题