大佬教程收集整理的这篇文章主要介绍了ios – UITableView-Swift:UITableViewCell中的Checkmark,大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
- (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; } }
这是一些清理:
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所遇到的程序开发问题。
如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。
本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。