我知道如何处理单元格类型的复选标记,这里我想要一个按钮来取消复选标记的整个表格单元?这里是我的代码,有没有人可以帮我写代码
- (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];
}发布于 2012-08-18 13:56:19
如果你在didSelectRowAtIndexPath:方法上做复选标记。reloadData将为您工作。
例如。
假设您有一个带有选择器uncheckCells的按钮
-(void)uncheckCells
{
[self.tableView reloadData];
}这对你来说应该很有效。
编辑:在if (cell == nil)条件中添加cell.accessoryType = UITableViewCellAccessoryNone;。
或者执行以下操作:
-(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;
}
}
}我希望它能起作用。
发布于 2012-08-18 15:01:43
从您的代码中可以看出,您正在对表视图本身设置复选标记。
您真正应该做的是存储数据源中的单元格是否具有复选标记,并在tableView:cellForRowAtIndexPath:方法中使用此信息来显示复选标记。
您的取消选择方法应该遍历您的数据源,并取消设置表示您的表视图是否具有复选标记的任何标记。
这样,当您滚动一个表视图,或者只是调用[tableView reloadData];时,您的数据存储将用于决定是否显示复选标记。
https://stackoverflow.com/questions/12016168
复制相似问题