要按照时间戳对多个TableViewCell类型进行排序,可以按照以下步骤进行:
class CustomTableViewCell: UITableViewCell {
var timestamp: TimeInterval?
// 其他属性...
}
var cellDataArray = [CustomTableViewCell]()
let cell1 = CustomTableViewCell()
cell1.timestamp = 1592345678
let cell2 = CustomTableViewCell()
cell2.timestamp = 1593456789
// 将TableViewCell对象添加到数组中
cellDataArray.append(cell1)
cellDataArray.append(cell2)
sorted(by:)
方法根据时间戳对TableViewCell对象进行排序。cellDataArray.sort { $0.timestamp ?? 0 < $1.timestamp ?? 0 }
cellForRow(at:)
方法中使用排序后的数组来获取正确的TableViewCell对象。func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "CustomCell", for: indexPath) as! CustomTableViewCell
let rowData = cellDataArray[indexPath.row]
// 使用rowData配置TableViewCell的显示
return cell
}
这样,TableView就会按照时间戳对TableViewCell进行排序。请注意,上述代码中的时间戳是一个示例,您需要根据实际情况使用正确的时间戳。