
我必须在我的UItableVIEw中重现与Settings-> General中的auto-lock选项相同的行为.在Objective-C中,我写过
- (UItableVIEwCell *)tableVIEw:(UItableVIEw *)tableVIEw cellForRowAtIndexPath:(NSIndexPath *)indexPath { static Nsstring *checkmarkCellIDentifIEr = @"checkmarkCellIDentifIEr"; UItableVIEwCell *cell = [tableVIEw dequeueReusableCellWithIDentifIEr: checkmarkCellIDentifIEr]; if (cell == nil) { cell = [[[UItableVIEwCell alloc] initWithStyle:UItableVIEwCellStyleSubTitle reuseIDentifIEr:checkmarkCellIDentifIEr] autorelease]; } cell.textLabel.text = ... cell.detailTextLabel.text = ...; cell.accessoryType = [indexPath isEqual: self.lastSelectedindexPath] ? UItableVIEwCellAccessorycheckmark : UItableVIEwCellAccessoryNone; return cell;- (voID)tableVIEw:(UItableVIEw *)tableVIEw dIDSelectRowAtIndexPath:(NSIndexPath *)indexPath { int newRow = indexPath.row; int oldRow = self.lastSelectedindexPath.row; if (newRow != oldRow) { UItableVIEwCell *newCell = [tableVIEw cellForRowAtIndexPath:indexPath]; newCell.accessoryType = UItableVIEwCellAccessorycheckmark; UItableVIEwCell *oldCell = [tableVIEw cellForRowAtIndexPath:self.lastSelectedindexPath]; oldCell.accessoryType = UItableVIEwCellAccessoryNone; self.lastSelectedindexPath = indexPath; } [tableVIEw deselectRowAtIndexPath:indexPath animated:YES];} 这很棒!
现在,在Swift中,我写道:
overrIDe func vIEwDIDLoad() { super.vIEwDIDLoad() ... self.tableVIEw.registerClass(UItableVIEwCell.self,forCellReuseIDentifIEr: "categoryCell")}func tableVIEw(tableVIEw: UItableVIEw,cellForRowAtIndexPath indexPath: NSIndexPath) -> UItableVIEwCell { var cell: UItableVIEwCell = tableVIEw.dequeueReusableCellWithIDentifIEr("categoryCell",forIndexPath: indexPath) as UItableVIEwCell cell.textLabel?.text = categorIEs[indexPath.row] let row = indexPath.row; if let lastIndexPath = self.lastSelectedindexPath { cell.accessoryType = (lastIndexPath.row == row) ? UItableVIEwCellAccessoryType.checkmark : UItableVIEwCellAccessoryType.None; } return cell}func tableVIEw(tableVIEw: UItableVIEw,dIDSelectRowAtIndexPath indexPath: NSIndexPath) { self.tableVIEw.deselectRowAtIndexPath(indexPath,animated: true) let newRow = indexPath.row; var oldRow: Int? if let lastIndexPath = self.lastSelectedindexPath { oldRow = lastIndexPath.row; let oldCell = self.tableVIEw(tableVIEw,cellForRowAtIndexPath: lastIndexPath) oldCell.accessoryType = UItableVIEwCellAccessoryType.None; } if (newRow != oldRow) { let newCell = self.tableVIEw(tableVIEw,cellForRowAtIndexPath: indexPath) newCell.accessoryType = UItableVIEwCellAccessoryType.checkmark; self.lastSelectedindexPath = indexPath; }} 这不起作用.复选标记有时仅出现,当我滚动时它会消失.我花了两个小时才弄明白为什么但没有成功.
解决方法 主要问题是你调用tableVIEw(_:cellForRowAtIndexPath :)而不是tableVIEw.cellForRowAtIndexPath(_ :),因此创建一个新的单元格而不是获取当前显示的单元格.这是一些清理:
overrIDe func vIEwDIDLoad() { super.vIEwDIDLoad() // … tableVIEw.registerClass(UItableVIEwCell.self,forCellReuseIDentifIEr: "categoryCell")}func tableVIEw(tableVIEw: UItableVIEw,cellForRowAtIndexPath indexPath: NSIndexPath) -> UItableVIEwCell { let cell = tableVIEw.dequeueReusableCellWithIDentifIEr("categoryCell",forIndexPath: indexPath) as UItableVIEwCell cell.accessoryType = (lastSelectedindexPath?.row == indexPath.row) ? .checkmark : .None cell.textLabel?.text = categorIEs[indexPath.row] return cell}func tableVIEw(tableVIEw: UItableVIEw,dIDSelectRowAtIndexPath indexPath: NSIndexPath) { tableVIEw.deselectRowAtIndexPath(indexPath,animated: true) if indexPath.row != lastSelectedindexPath?.row { if let lastSelectedindexPath = lastSelectedindexPath { let oldCell = tableVIEw.cellForRowAtIndexPath(lastSelectedindexPath) oldCell?.accessoryType = .None } let newCell = tableVIEw.cellForRowAtIndexPath(indexPath) newCell?.accessoryType = .checkmark lastSelectedindexPath = indexPath }} 总结 以上是内存溢出为你收集整理的ios – UITableView-Swift:UITableViewCell中的Checkmark全部内容,希望文章能够帮你解决ios – UITableView-Swift:UITableViewCell中的Checkmark所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
微信扫一扫
支付宝扫一扫
评论列表(0条)