uitaiteViewCell选中标记更改选择
-
04-10-2019 - |
题
我认为要更改“ on”为“ OFF”的复选标记,我必须更改 CellAccessoryType
之间 none
和 checkmark
在 didSelectRowAtIndexPath
?
因为我已经这样做了,但我注意到这种行为与iPhone上自动锁定设置上的Checkmark单元格并不完全相同。
还是有其他方法可以处理的其他方式?
解决方案
将属性保存在您的视图控制器中
selectedRow
, ,代表一行代表表部分中检查项目的索引。在您的视图控制器中
-tableView:cellForRowAtIndexPath:
委托法,设置accessoryType
的cell
到UITableViewCellAccessoryCheckmark
如果细胞的indexPath.row
等于selectedRow
价值。否则,将其设置为UITableViewCellAccessoryNone
.在您的视图控制器中
-tableView:didSelectRowAtIndexPath:
委托法,设置selectedRow
价值indexPath.row
选择的,例如:self.selectedRow = indexPath.row
其他提示
另一个解决方案:
-(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];
在
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
它应该是
didhighlightrowatindExpath
代替
TableView:DidSelectRowatIndExpath
Alex Answer仅在添加重新加载表之后才适用于我在.h
{
int selectedCell;
}
@property(nonatomic,readwrite)int selectedCell;
在.m *CellForrowatIndExpath*
if(indexPath.row == selectedCell)
{
cell.accessoryType = UITableViewCellAccessoryCheckmark;
cell.selected = YES;
}
else
{
cell.accessoryType = UITableViewCellAccessoryNone;
cell.selected = NO;
}
以及在任何didhighlightrowatindExpath或didSelectRowatIndExpath的任何地方
self.selectedCell = indexPath.row;
[self.tableView reloadData];
迅速:
var selectedCell:UITableViewCell?{
willSet{
selectedCell?.accessoryType = .None
newValue?.accessoryType = .Checkmark
}
}
然后:
override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
selectedCell = self.tableView.cellForRowAtIndexPath(indexPath)
self.mapView.selectAnnotation(self.mapView.annotations[indexPath.row], animated: true)
}
如果要使用自定义映像作为辅助类型,请使用以下代码
-(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;
}
对于Swift
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
}