在UITableView IOS中选择行时多个复选标记
我有一个在UITableView IOS中选择行时多个复选标记,ios,objective-c,uitableview,didselectrowatindexpath,Ios,Objective C,Uitableview,Didselectrowatindexpath,我有一个UITableView,它在选中一行时显示复选标记。问题是,当我在didselectrowatinexpath中选择一行并在所选行上添加复选标记时,它会添加一个额外的复选标记。这是我的密码 任何帮助都将不胜感激 - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { UITableViewCell *cell = [table
UITableView
,它在选中一行时显示复选标记。问题是,当我在didselectrowatinexpath
中选择一行并在所选行上添加复选标记时,它会添加一个额外的复选标记。这是我的密码
任何帮助都将不胜感激
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"Cell" forIndexPath:indexPath];
// Configure the cell...
cell.textLabel.text=[[Model.category objectAtIndex:indexPath.row] categoryName];
cell.imageView.image=[[Model.category objectAtIndex:indexPath.row]categoryImage];
//cell.detailTextLabel.text =@"Breve Descripción de la categoria";
return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
if ([self.tableView cellForRowAtIndexPath:indexPath].accessoryType == UITableViewCellAccessoryCheckmark) {
[self.tableView cellForRowAtIndexPath:indexPath].accessoryType =UITableViewCellAccessoryNone;
[self.cellSelected removeObject:indexPath];
}else {
[tableView cellForRowAtIndexPath:indexPath].accessoryType=UITableViewCellAccessoryCheckmark;
[self.cellSelected addObject:indexPath];
}
[self checkMark];
[tableView reloadData];
}
- (void)checkMark{
for (NSIndexPath * indexPath in self.cellSelected) {
[self.tableView cellForRowAtIndexPath:indexPath].accessoryType=UITableViewCellAccessoryCheckmark;
}
}
试试这个:
在.m文件中声明:
@interface MyViewController () {
NSIndexPath *__selectedPath;
}
在tableView:cellforrowatinexpath:
中,检查给定的indexPath是否与存储在ivar中的相同:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
//configure cell
if ([__selectedPath isEqual:indexPath]) {
cell.accessoryType = UITableViewCellAccessoryCheckmark;
} else {
cell.accessoryType = UITableViewCellAccessoryNone;
}
return cell;
}
在tableView中:didselectRowatineXpath
存储指向所选NSIndexPath的指针,如果已取消选择单元格,则存储为nil
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
if (cell.accessoryType == UITableViewCellAccessoryNone) {
cell.accessoryType = UITableViewCellAccessoryCheckmark;
__selectedPath = indexPath;
} else {
cell.accessoryType = UITableViewCellAccessoryNone;
__selectedPath = nil;
}
}
我没有签入Xcode就写了它,所以可能会有一些拼写错误,但我展示了主要思想。
在我看来,你不应该打电话给[自我检查标记]在表视图中的code>:cellforrowatinexpath:
方法
此外,如果希望一次只选择一个单元格,则不应创建
NSMutableArray
来存储nsindepath
。您的cellSelected
似乎一次存储2个nsindexpath
,这就是您出现这种奇怪行为的原因。[self.tableView cellForRowAtIndexPath:indexath]
调用didSelectRowAtIndexPath
不会返回准确的单元格。它可以是相同的单元、新单元或重复使用的单元。如果它是一个在其附件视图中有复选标记的重复使用的单元格,则最终将有两个带有复选标记的单元格
最好存储在数组中并相应地使用它。如果您计划有多个选择,请使用下面的代码示例
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
self.cellSelected = [NSMutableArray array];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
//Cell Initialisation here
if ([self.cellSelected containsObject:indexPath])
{
cell.accessoryType = UITableViewCellAccessoryCheckmark;
}
else
{
cell.accessoryType = UITableViewCellAccessoryNone;
}
return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
[tableView deselectRowAtIndexPath:indexPath animated:YES];
//if you want only one cell to be selected use a local NSIndexPath property instead of array. and use the code below
//self.selectedIndexPath = indexPath;
//the below code will allow multiple selection
if ([self.cellSelected containsObject:indexPath])
{
[self.cellSelected removeObject:indexPath];
}
else
{
[self.cellSelected addObject:indexPath];
}
[tableView reloadData];
}
非常感谢,但是我需要调用方法checkMark,因为我有一个带有indexPath的可变数组,具体取决于选择的单元格,我需要那些带有复选标记的单元格。我是新手,希望你能帮助我。如果你想一次只选择一个单元格,不要使用NSMutableArray。就这么简单。NSMutableArray占用了更多的内存空间,因此这里不需要使用它。此外,在使用NSIndexPath时,您将确保只存储一个指向NSIndexPath的指针,并且在tableView重新加载数据期间,只选择一个或零个UITableViewCell。为什么需要[tableView DeserrowatingIndexPath:indexPath animated:YES]??当我对这行进行注释时,效果也很好。我也有同样的问题,但对于图像,每个单元格都有一个图像,当我选择一行时,我需要更改所选行的图像,但是当我更改此图像时,另外,您可以帮助medeselect的另一个图像更改是,当您触摸单元格时,删除突出显示的行为。Klingert0728需要查看您的代码,以找到您面临的实际问题。与其重新加载整个tableView,不如使用“reloadRowsAtIndexPaths”重新加载所选行?