【发布时间】:2016-04-06 12:23:08
【问题描述】:
didDeselectRowAtIndexPath 委托函数有问题。
当我第一次选择时,didDeselectRowAtIndexPath 不会被调用。
但是当我选择另一个单元格时,didDeselectRowAtIndexPath 会被调用,但带有上一个选择的索引路径。
举个例子:
- 我选择了索引 5 处的行。所以什么都不会发生。
- 我已选择索引 12 处的行,然后将使用 indexPath.row = 5 调用
didDeselectRowAtIndexPath。
我不知道为什么会这样,这是我的代码:谢谢
class testUICollectionView: UIViewController ,UITableViewDelegate,UITableViewDataSource {
let tableItems :NSMutableArray = ["Item 1", "Item 2", "Item 3", "Item 4", "Item 5",
"Item 6", "Item 7", "Item 8", "Item 9", "Item 10", "Item 11",
"Item 12", "Item 13", "Item 14", "Item 15","Item 12", "Item 13", "Item 14", "Item 15","Item 12", "Item 13", "Item 14", "Item 15"]
var tableView:UITableView!;
override func viewDidLoad() {
super.viewDidLoad()
self.view.backgroundColor = UIColor.blueColor();
tableView = UITableView();
tableView.frame = self.view.frame;
tableView.registerClass(TableViewCell.self, forCellReuseIdentifier: "Cell");
tableView.dataSource = self;
tableView.delegate = self;
self.view.addSubview(tableView)
}
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
// Return the number of rows in the section.
return tableItems.count
}
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCellWithIdentifier("Cell", forIndexPath: indexPath) as! TableViewCell
cell.setTextLabel(tableItems[indexPath.row] as! String);
return cell
}
func tableView(tableView: UITableView, didDeselectRowAtIndexPath indexPath: NSIndexPath) {
print("inexPath.row : " + String(indexPath.row));
}
}
////////////////////////// table view cell
class TableViewCell:UITableViewCell{
var label:UILabel!;
func setTextLabel(new_text:String){
if (label == nil){
label = UILabel();
label.frame = self.contentView.frame;
label.textColor = UIColor.whiteColor();
label.backgroundColor = UIColor.whiteColor();
label.textColor = UIColor.blackColor();
self.contentView.addSubview(label);
}
label.text = new_text;
}
}
【问题讨论】:
-
即使我对您的代码也有同样的问题。
标签: ios swift uitableview ios9