【发布时间】:2018-05-27 10:57:48
【问题描述】:
在这里,我想使用 index.row 来索引 Array,但奇怪的是 index.row 会在我滚动 tableView 时重新计数。有什么解决办法吗?或另一种索引数组的方法?
如下:
0
1
2
3
4
5
6
7
8
9
10
11
12
13
14
4 #recount from 4
5
6
7
8
9
10
这里是源代码:
func tableviewSetUp() {
let rect = self.view.frame
self.tableView = UITableView(frame: rect)
self.tableView.backgroundColor = UIColor.black
self.tableView.delegate = self
self.tableView.dataSource = self
self.view.addSubview(self.tableView)
self.tableView.register(WallpaperTableViewCell.self, forCellReuseIdentifier: "wallpaperCell")
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "wallpaperCell", for: indexPath) as! WallpaperTableViewCell
print(indexPath.row) //here I print the row number
let wallpaper = self.wallpaperIDList[indexPath.row]//here I using the index.row to index the array
return cell
}
func numberOfSections(in tableView: UITableView) -> Int {
return 1
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return 50
}
【问题讨论】:
-
这就是cell复用的核心。一旦单元格从屏幕上滚出,它就会被重复使用,并且在您向后滚动时必须重新设置。
-
@Sulthan 嗯,但这种重新计票情况只发生一次。从 4 开始重新计数后,将永远不会重新计数,并一直计数到 50。
-
@OttoYing:调用数据源方法的频率和顺序无关紧要。如果这会影响您的程序,那么您在其他地方会出现编程错误。
-
目前还不清楚你的实际问题是什么......
-
当
cellForRowAt正在索引self.wallpaperIDList时,为什么numberOfRowsInSection用50硬编码?您的numberOfRowsInSection需要返回self.wallpaperIDList.count。
标签: ios swift uitableview