Objective c UITableViewCell在选择时选中标记更改
我认为要将“开”的复选标记更改为“关”,我必须在Objective c UITableViewCell在选择时选中标记更改,objective-c,iphone,tablecell,checkmark,Objective C,Iphone,Tablecell,Checkmark,我认为要将“开”的复选标记更改为“关”,我必须在didSelectRowAtIndexPath上的CellAccessoryType和checkmark之间更改 因为我已经这样做了,但我注意到这种行为与iphone上自动锁定设置上的复选标记单元格并不完全相同 还是有其他方法来处理复选标记 在视图控制器中保留一个名为selectedRow的属性,该属性表示一行的索引,该行表示表节中的选中项 在视图控制器的-tableView:cellforrowatinexpath:委托方法中,如果单元格的ind
didSelectRowAtIndexPath
上的CellAccessoryType
和checkmark
之间更改
因为我已经这样做了,但我注意到这种行为与iphone上自动锁定设置上的复选标记单元格并不完全相同
还是有其他方法来处理复选标记
在视图控制器中保留一个名为selectedRow
的属性,该属性表示一行的索引,该行表示表节中的选中项
在视图控制器的-tableView:cellforrowatinexpath:
委托方法中,如果单元格的indexPath.row
等于selectedRow
值,则将单元格的accessoryType
设置为UITableViewCellAccessoryCheckmark
。否则,将其设置为UITableViewCellAccessorOne
在视图控制器的-tableView:didselectedrow=indexPath:
委托方法中,将selectedRow
值设置为选中的indexPath.row
,例如:self.selectedRow=indexPath.row
应该是
didHighlightRowAtIndexPath
而不是
tableView:didSelectRowAtIndexPath
Alex answer只在添加重新加载表后才为我工作,
in.h
{
int selectedCell;
}
@property(nonatomic,readwrite)int selectedCell;
self.selectedCell = indexPath.row;
[self.tableView reloadData];
in.m
*cellForRowAtIndexPath*
并在任意位置选择HighlightRowatineXpath或选择RowatineXpath
{
int selectedCell;
}
@property(nonatomic,readwrite)int selectedCell;
self.selectedCell = indexPath.row;
[self.tableView reloadData];
另一个解决方案:
-(NSIndexPath *)tableView:(UITableView *)tableView willSelectRowAtIndexPath:(NSIndexPath *)indexPath {
NSIndexPath *oldIndex = [self.tableView indexPathForSelectedRow];
[self.tableView cellForRowAtIndexPath:oldIndex].accessoryType = UITableViewCellAccessoryNone;
[self.tableView cellForRowAtIndexPath:indexPath].accessoryType = UITableViewCellAccessoryCheckmark;
return indexPath;
}
是的:您不必检查oldIndex
是否为nil
:)
Swift 3及更高版本:
override func tableView(_ tableView: UITableView, willSelectRowAt indexPath: IndexPath) -> IndexPath? {
if let oldIndex = tableView.indexPathForSelectedRow {
tableView.cellForRow(at: oldIndex)?.accessoryType = .none
}
tableView.cellForRow(at: indexPath)?.accessoryType = .checkmark
return indexPath
}
Zyphrax提出了一个对我非常有效的解决方案!如果需要清除上一选定行,只需使用:
[self.tableView reloadData];
在
如果要将自定义图像用作附件类型,请使用下面的代码
-(NSIndexPath *)tableView:(UITableView *)tableView willSelectRowAtIndexPath:(NSIndexPath *)indexPath {
UIImageView *imgCheckMark = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"select_icon.png"]];
[imgCheckMark setFrame:CGRectMake(self.view.frame.size.width - 30, 25, 14, 18)];
imgCheckMark.tag = 1000+indexPath.row;
NSIndexPath *oldIndex = [self.tblSelectService indexPathForSelectedRow];
[self.tblSelectService cellForRowAtIndexPath:oldIndex].accessoryView = UITableViewCellAccessoryNone;
[self.tblSelectService cellForRowAtIndexPath:indexPath].accessoryView = imgCheckMark;
return indexPath;
}
斯威夫特:
然后:
斯威夫特
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
tableView.cellForRow(at: indexPath)?.accessoryType = .checkmark
}
func tableView(_ tableView: UITableView, didDeselectRowAt indexPath: IndexPath) {
tableView.cellForRow(at: indexPath)?.accessoryType = .none
}
对不起,我不是说我有多个复选标记单元格(更像是单选按钮)。只是它们的动画没有那么流畅(但我现在已经修复了)。但是你的回答意味着我正在使用正确的方法:)添加到Alex的解决方案中-为了在点击单元格时显示复选标记,你还必须在设置所选单元格后输入[tableview reloadData]。这很简单,但将数据重新加载到整个表中可能会影响性能。因为您知道需要更新什么indexath.row
,所以只需要更新该行,这可以通过表视图方法-reloadRowsAtIndexPaths:withRowAnimation:
完成。有关更多详细信息:@AlexReynolds,如果单元格的indexPath.row等于selectedRow值,请详细说明您何时说过将单元格的accessoryType设置为UITableViewCellAccessoryCheckmark。否则,将其设置为UITableViewCellAccessoryNone。该行需要或不需要复选标记。如果是所选行(通过比较索引路径行和所选行值),则标记它。否则,不要这样做。这真是一个伟大的代码。我添加了Swift版本。非常好的解决方法。非常感谢。这是最直接和正确的答案。
override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
selectedCell = self.tableView.cellForRowAtIndexPath(indexPath)
self.mapView.selectAnnotation(self.mapView.annotations[indexPath.row], animated: true)
}
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
tableView.cellForRow(at: indexPath)?.accessoryType = .checkmark
}
func tableView(_ tableView: UITableView, didDeselectRowAt indexPath: IndexPath) {
tableView.cellForRow(at: indexPath)?.accessoryType = .none
}