Jim*_*ery 1 uitableview ios swift
在TableViewController中我有这个代码:
override func viewDidLoad(){
super.viewDidLoad()
self.tab = UITableView(frame: CGRectMake(8, 80, 584, 512), style: UITableViewStyle.Plain)
self.tab?.delegate = self
self.tab?.dataSource = self
self.tab?.registerClass(CustomCell.self, forCellReuseIdentifier: "cell")
self.tab?.reloadData()
self.view.addSubview(tab!)
}
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
if let count = self.data.count {
return count
}
return 0
}
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
var cell = tableView.dequeueReusableCellWithIdentifier("cell", forIndexPath: indexPath) as? CustomCell
if cell == nil {
cell = CustomCell(style: UITableViewCellStyle.Value1, reuseIdentifier: "cell")
}
if let item = self.data[indexPath.row] as DataItem? {
// cell.updateWithDataItem(item)
// this line is commented out because the error occurs with or without this line
}
return cell
}
Run Code Online (Sandbox Code Playgroud)
在我的CustomCell中,我有这个代码:
override func setSelected(selected: Bool, animated: Bool){
super.setSelected(selected, animated: animated)
println("cell selected \(id)")
}
Run Code Online (Sandbox Code Playgroud)
当我从服务器获取我的JSON数据时,我在TableViewController中有这个代码填充表self.tab:
self.data = JSONData
dispatch_async(dispatch_get_main_queue(), {
self.tab?.reloadData()
})
Run Code Online (Sandbox Code Playgroud)
我的问题是,当我填充self.tab我的UITableView时,我最终会在控制台中使用它
cell selected f4324
cell selected f4325
cell selected f4326
cell selected f4327
cell selected f4328
...... and so on .....
Run Code Online (Sandbox Code Playgroud)
就好像正在选择单元格,因为它们被添加到我的UITableView中.
这是UITableView的典型行为吗?
我想停止这种行为,并且只有响应用户交互的代码(不是正在填充的表).我怎样才能做到这一点?
小智 15
是的,这是UITableView和UITableViewCell的正常行为.使其响应用户交互.您可以实现UITableView的委托方法:
func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath);
Run Code Online (Sandbox Code Playgroud)
当用户点击单元格时,将调用此函数.
| 归档时间: |
|
| 查看次数: |
8735 次 |
| 最近记录: |