我试图禁用用户与表视图中某些行的交互(并使它们稍微褪色)。根据数组中显示的日期,仅第一行应被禁用,因为索引值应仅为0,但由于某种原因我的第五行也被禁用。不知道为什么。
var datesAndYear = ["2019-01-14", "2019-04-30", "2019-05-7", "2019-05-14", "2019-05-21", "2019-05-28"]
var index: Int?
override func viewDidLoad() {
// today's date
let currentDateTime = Date()
let dateFormatter = DateFormatter()
dateFormatter.dateFormat = "yyyy-MM-dd"
for i in 0..<6 {
let date = dateFormatter.date(from: datesAndYear[i])
if (currentDateTime >= date!) {
self.index = i
}
}
}
扩展名
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath) as? HomeTableViewCell
if index != nil {
if (indexPath.row <= index!) {
cell?.isUserInteractionEnabled = false
cell?.contentView.alpha = 0.5
}
}
return cell!
...
}