iOS   发布时间:2022-03-30  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了ios – UITableView-Swift:UITableViewCell中的Checkmark大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
@H_675_1@
我正在使用 Xcode 6.0.1,iOS8和 Swift.
我必须在我的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所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。