I want to add a check sign when any row is selected in table view please help
我想在表格视图中选择任何行时添加一个复选标记,请帮忙
2 个解决方案
#1
4
Implement tableView:didSelectRowAtIndexPath:
in your table view delegate, grab the cell that was selected using [tableView cellForRowAtIndexPath:indexPath]
and set its accessory to UITableViewCellAccessoryCheckmark
.
在表视图委托中实现tableView:didSelectRowAtIndexPath:获取使用[tableView cellForRowAtIndexPath:indexPath]选择的单元格,并将其附件设置为UITableViewCellAccessoryCheckmark。
If you want to implement something similar to the multi-value table views in the Settings app, what I do is use a property to keep track of what's already selected:
如果你想在Settings应用程序中实现类似于多值表视图的东西,我所做的就是使用属性来跟踪已经选择的内容:
@property (nonatomic, retain) NSIndexPath *selectedIndexPath;
In the same delegate method, I remove the checkmark from the cell at the selection, add the checkmark to the cell that was tapped and update the property with the index of that cell:
在相同的委托方法中,我从选择的单元格中删除复选标记,将复选标记添加到被轻击的单元格,并使用该单元格的索引更新属性:
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewCell *oldCell = [tableView cellForRowAtIndexPath:self.selectedIndexPath];
[oldCell setAccessory:UITableViewCellAccessoryNone];
UITableViewCell *newCell = [tableView cellForRowAtIndexPath:indexPath];
[newCell setAccessory:UITableViewCellAccessoryCheckmark];
[self setSelectedIndexPath:indexPath];
[tableView deselectRowAtIndexPath:indexPath animated:YES];
}
To go one step further, you can find a way to persist the index path in your app (possibly using NSUserDefaults
, and in tableView:cellForRowAtIndexPath:
apply the checkmark to whichever cell that corresponds to that index path, so your checkmark shows up on view load.
更进一步,您可以找到一种在应用程序中保留索引路径的方法(可能使用NSUserDefaults,并在tableView中:cellForRowAtIndexPath:将复选标记应用于与该索引路径对应的任何单元格,因此您的复选标记显示在视图中加载。
#1
4
Implement tableView:didSelectRowAtIndexPath:
in your table view delegate, grab the cell that was selected using [tableView cellForRowAtIndexPath:indexPath]
and set its accessory to UITableViewCellAccessoryCheckmark
.
在表视图委托中实现tableView:didSelectRowAtIndexPath:获取使用[tableView cellForRowAtIndexPath:indexPath]选择的单元格,并将其附件设置为UITableViewCellAccessoryCheckmark。
If you want to implement something similar to the multi-value table views in the Settings app, what I do is use a property to keep track of what's already selected:
如果你想在Settings应用程序中实现类似于多值表视图的东西,我所做的就是使用属性来跟踪已经选择的内容:
@property (nonatomic, retain) NSIndexPath *selectedIndexPath;
In the same delegate method, I remove the checkmark from the cell at the selection, add the checkmark to the cell that was tapped and update the property with the index of that cell:
在相同的委托方法中,我从选择的单元格中删除复选标记,将复选标记添加到被轻击的单元格,并使用该单元格的索引更新属性:
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewCell *oldCell = [tableView cellForRowAtIndexPath:self.selectedIndexPath];
[oldCell setAccessory:UITableViewCellAccessoryNone];
UITableViewCell *newCell = [tableView cellForRowAtIndexPath:indexPath];
[newCell setAccessory:UITableViewCellAccessoryCheckmark];
[self setSelectedIndexPath:indexPath];
[tableView deselectRowAtIndexPath:indexPath animated:YES];
}
To go one step further, you can find a way to persist the index path in your app (possibly using NSUserDefaults
, and in tableView:cellForRowAtIndexPath:
apply the checkmark to whichever cell that corresponds to that index path, so your checkmark shows up on view load.
更进一步,您可以找到一种在应用程序中保留索引路径的方法(可能使用NSUserDefaults,并在tableView中:cellForRowAtIndexPath:将复选标记应用于与该索引路径对应的任何单元格,因此您的复选标记显示在视图中加载。