Han*_*kke 42 iphone uitableview
如何在我的基于视图的应用程序中添加UITableView,用户将点击多个单元格,它将被选中,就像Clock应用程序的"New Alarm"设置名为"Repeat"(Clock> Alarms> +>重复),我怎样才能获得数组中的所有选定单元格?
Rap*_*ira 129
要进行多项选择,请在下方添加以下行 viewDidLoad()
tableView.allowsMultipleSelection = true
Run Code Online (Sandbox Code Playgroud)
cell在将其出列(或初始化)之后配置每个tableView(_:cellForRowAt:)
let selectedIndexPaths = tableView.indexPathsForSelectedRows
let rowIsSelected = selectedIndexPaths != nil && selectedIndexPaths!.contains(indexPath)
cell.accessoryType = rowIsSelected ? .checkmark : .none
// cell.accessoryView.hidden = !rowIsSelected // if using a custom image
Run Code Online (Sandbox Code Playgroud)
选中/取消选中时,更新每个单元格
override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
let cell = tableView.cellForRow(at: indexPath)!
cell.accessoryType = .checkmark
// cell.accessoryView.hidden = false // if using a custom image
}
override func tableView(_ tableView: UITableView, didDeselectRowAt indexPath: IndexPath) {
let cell = tableView.cellForRow(at: indexPath)!
cell.accessoryType = .none
// cell.accessoryView.hidden = true // if using a custom image
}
Run Code Online (Sandbox Code Playgroud)
完成后,获取所有选定行的数组
let selectedRows = tableView.indexPathsForSelectedRows
Run Code Online (Sandbox Code Playgroud)
并获取所选数据,其中dataArray映射到只有1个部分的表视图的行
let selectedData = selectedRows?.map { dataArray[$0.row].ID }
Run Code Online (Sandbox Code Playgroud)
Bre*_*erg 28
在您的实现中,-tableView:didSelectRowAtIndexPath:您将accessoryType根据其当前值设置表视图单元格的属性(因此它将通过多次点击打开和关闭).例如:
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)path {
UITableViewCell *cell = [tableView cellForRowAtIndexPath:path];
if (cell.accessoryType == UITableViewCellAccessoryCheckmark) {
cell.accessoryType = UITableViewCellAccessoryNone;
} else {
cell.accessoryType = UITableViewCellAccessoryCheckmark;
}
}
Run Code Online (Sandbox Code Playgroud)
除了单元格自己的附件类型状态之外,您还可以维护一组选定状态,或者在表视图中迭代查询每个状态的单元格以读取所选行.
Dmi*_*sev 18
@BrendanBreg 实现对我没用.@RaphaelOliveira提供了很好的解决方案,但是当您向下滚动表格时 - 选择了错误的行(因为UITableView缓存了它的单元格).所以,我稍微修改了拉斐尔的解决方案:
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell* cell = [tableView cellForRowAtIndexPath:indexPath];
cell.accessoryType = UITableViewCellAccessoryCheckmark;
}
- (void)tableView:(UITableView *)tableView didDeselectRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell* cell = [tableView cellForRowAtIndexPath:indexPath];
cell.accessoryType = UITableViewCellAccessoryNone;
}
/*Here is modified part*/
- (UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
/*
...
Your implementation stays here
we're just adding few lines to make sure
that only correct rows will be selected
*/
if([[tableView indexPathsForSelectedRows] containsObject:indexPath]) {
cell.accessoryType = UITableViewCellAccessoryCheckmark;
} else {
cell.accessoryType = UITableViewCellAccessoryNone;
}
}
Run Code Online (Sandbox Code Playgroud)
sou*_*ael 14
除了上面的答案之外,还有一个快速提示:从时钟应用程序模仿Apple的风格(在检查/取消选中行后使行选择颜色淡出),didSelectRowAtIndexPath在条件之后将其添加到:
[self.tableView deselectRowAtIndexPath:indexPath animated:YES];
Run Code Online (Sandbox Code Playgroud)
来自Apple的TableMultiSelect指南.
| 归档时间: |
|
| 查看次数: |
57561 次 |
| 最近记录: |