[英]didSelectRowAtIndexPath selecting multiple tableView cell accessory's
[英]IOS: didSelectRowAtIndexPath select multiple cells while selecting single cell
我有一个UITableView,其中填充了30多个UITableviewCells。
我面临的问题是,当选择一行并更改单元格的annexType时,该行的第11行也将被修改。
我尝试打印[indexPath row]
值,但它仅显示所选的行,而不显示另一行。
我正在使用以下代码:
-(UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *simpleTableIdentifier = @"SimpleTableItem";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier];
if (cell == nil)
{
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:simpleTableIdentifier];
}
cell.textLabel.font=[UIFont fontWithName: @"Arial" size: 14.0];
cell.textLabel.lineBreakMode = NSLineBreakByWordWrapping;
.
.
.
}
请帮助我如何克服这个问题
您需要跟踪选定的单元格,您可以使用NSMutableDictionary作为键,使用IndexPath.row并使用String作为值,因此请在DidSelectRow中修改字典,将Row =“ Selected”的值设置为您的CellForRow方法检查如果Dictionary [indexPath.row] ==“已选择”,则默认将您的配件删除
@interface ViewController () <UITableViewDataSource,UITableViewDelegate>
@property NSMutableDictionary * dict;
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
_dict = [NSMutableDictionary dictionary];
}
-(UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *simpleTableIdentifier = @"SimpleTableItem";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier];
if (cell == nil)
{
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:simpleTableIdentifier];
}
cell.accessoryType = UITableViewCellAccessoryNone;
if([((NSString*)[_dict objectForKey:[NSString stringWithFormat:@"%li",(long)indexPath.row]]) isEqualToString: @"Selected"])
{
cell.accessoryType = UITableViewCellAccessoryCheckmark;
}
cell.textLabel.font=[UIFont fontWithName: @"Arial" size: 14.0];
cell.textLabel.lineBreakMode = NSLineBreakByWordWrapping;
return cell;
}
在您的didSelectRowAtIndexPath方法中
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
NSString * value = (NSString*)[_dict objectForKey:[NSString stringWithFormat:@"%li",(long)indexPath.row]];
if ([value isEqualToString:@"Selected"]){
[_dict setValue:@"UnSelected" forKey:value];
}else{
[_dict setValue:@"Selected" forKey:[NSString stringWithFormat:@"%li",(long)indexPath.row]];
}
[tableView reloadRowsAtIndexPaths:@[indexPath,[NSIndexPath indexPathForRow:[value integerValue] inSection:0]] withRowAnimation:UITableViewRowAnimationNone];
}
希望这对您有帮助
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.