gpt4 book ai didi

ios - UITableView-Swift : Checkmark in UITableViewCell

转载 作者:搜寻专家 更新时间:2023-10-31 21:53:54 25 4
gpt4 key购买 nike

我正在使用 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 : Checkmark in UITableViewCell,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26238457/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com