Jon*_*an. 47 iphone objective-c tablecell checkmark
我是否正确地认为要将"on"的复选标记更改为"off",我必须更改CellAccessoryType
之间none
和之间checkmark
的didSelectRowAtIndexPath
?
因为我已经这样做但我注意到这个行为与iphone上自动锁定设置上的复选标记单元格完全相同.
或者还有其他一些方法可以处理检查标记吗?
Ale*_*lds 76
在视图控制器中保留一个属性,该属性selectedRow
表示表示表部分中已检查项的行的索引.
在视图控制器的-tableView:cellForRowAtIndexPath:
委托方法中,如果单元格等于该值,则设置accessoryType
为cell
to .否则,将其设置为.UITableViewCellAccessoryCheckmark
indexPath.row
selectedRow
UITableViewCellAccessoryNone
在视图控制器的-tableView:didSelectRowAtIndexPath:
委托方法中,将selectedRow
值设置indexPath.row
为所选的值,例如:self.selectedRow = indexPath.row
Zyp*_*rax 59
另一种方案:
-(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;
}
Run Code Online (Sandbox Code Playgroud)
和是:你没有检查是否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
}
Run Code Online (Sandbox Code Playgroud)
迅速:
var selectedCell:UITableViewCell?{
willSet{
selectedCell?.accessoryType = .None
newValue?.accessoryType = .Checkmark
}
}
Run Code Online (Sandbox Code Playgroud)
然后:
override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
selectedCell = self.tableView.cellForRowAtIndexPath(indexPath)
self.mapView.selectAnnotation(self.mapView.annotations[indexPath.row], animated: true)
}
Run Code Online (Sandbox Code Playgroud)
为了迅速
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
}
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
52180 次 |
最近记录: |