首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >Uitableview Accessorytype复选标记重置代码

Uitableview Accessorytype复选标记重置代码
EN

Stack Overflow用户
提问于 2012-08-18 13:42:56
回答 2查看 863关注 0票数 0

我知道如何处理单元格类型的复选标记,这里我想要一个按钮来取消复选标记的整个表格单元?这里是我的代码,有没有人可以帮我写代码

代码语言:javascript
运行
复制
- (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];
}

下面是我的函数的代码

代码语言:javascript
运行
复制
-(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];
}
EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2012-08-18 13:56:19

如果你在didSelectRowAtIndexPath:方法上做复选标记。reloadData将为您工作。

例如。

假设您有一个带有选择器uncheckCells的按钮

代码语言:javascript
运行
复制
-(void)uncheckCells
{
   [self.tableView reloadData];
}

这对你来说应该很有效。

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

或者执行以下操作:

代码语言:javascript
运行
复制
-(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;
        }
    }
}

我希望它能起作用。

票数 0
EN

Stack Overflow用户

发布于 2012-08-18 15:01:43

从您的代码中可以看出,您正在对表视图本身设置复选标记。

您真正应该做的是存储数据源中的单元格是否具有复选标记,并在tableView:cellForRowAtIndexPath:方法中使用此信息来显示复选标记。

您的取消选择方法应该遍历您的数据源,并取消设置表示您的表视图是否具有复选标记的任何标记。

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

票数 -1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/12016168

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档