[英]iOS tableView multiple selection
许多选择多行的方法或方法已被弃用或不起作用。
我只想在didSelectRowAtIndexPath
选择多行并将其保存到Array或NSUserDefault
。 在这种情况下,我选择了NSUserDefault
但我愿意将其更改为Array。
这是我的代码:
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
NotificationsTableViewCell *cell = (NotificationsTableViewCell*)[tableView cellForRowAtIndexPath:indexPath];
//set clicked icon after row selected
cell.imgIcon.image=[UIImage imageNamed:@"select-icon"];
//notification id for selected row
Notifications* selected = notifications[indexPath.section];
selectedGroup = selected.NotificationsId;
//save it in nsuserdefaults
[[NSUserDefaults standardUserDefaults] setObject:selectedGroup forKey:@"notificationSelectedID"];
}
-(void)tableView:(UITableView *)tableView didDeselectRowAtIndexPath:(NSIndexPath *)indexPath{
//deselect the row
}
注意 :我正在使用最新的Xcode SDK 8。
谢谢您的帮助。
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
NotificationsTableViewCell *cell = (NotificationsTableViewCell*)[tableView cellForRowAtIndexPath:indexPath];
// do your logic with following condition
if (cell.accessoryType == UITableViewCellAccessoryNone)
{
cell.accessoryType = UITableViewCellAccessoryCheckmark;
}
else
{
cell.accessoryType = UITableViewCellAccessoryNone;
}
}
您可以声明可变数组,并且每当用户每次选择特定行时,都为数组中的特定行添加所需的详细信息,然后使用NSUserDefaults存储更新后的数组。
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
[arrRows addObject:[NSString stringWithFormat:@"%@", indexPath]];
[[NSUserDefaults standardUserDefaults] setValue:arrRows forKey:@"arrRows"];
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.