我是否正确地认为要将“on”的复选标记更改为“off”,我必须更改CellAccessoryType
betweennone
和checkmark
on didSelectRowAtIndexPath
?
因为我已经这样做了,但我注意到该行为与 iphone 上自动锁定设置上的复选标记单元格并不完全相同。
还是有其他方法可以处理复选标记?
我是否正确地认为要将“on”的复选标记更改为“off”,我必须更改CellAccessoryType
betweennone
和checkmark
on didSelectRowAtIndexPath
?
因为我已经这样做了,但我注意到该行为与 iphone 上自动锁定设置上的复选标记单元格并不完全相同。
还是有其他方法可以处理复选标记?
在视图控制器中保留一个名为 的属性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
}
迅速:
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)
}
Zyphrax 提出了一个非常适合我的解决方案!如果您需要清除先前选择的行,只需使用:
[self.tableView reloadData];
在
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
它应该是
didHighlightRowAtIndexPath
代替
表视图:didSelectRowAtIndexPath
亚历克斯回答仅在.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];
为了迅速
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
}
如果您想使用自定义图像作为附件类型,请使用以下代码
-(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;
}