【问题标题】:UITableview reusable cell issue swiftUITableview可重用单元格问题迅速
【发布时间】:2016-05-13 18:34:57
【问题描述】:

我有一个表格视图,其中每个单元格都包含标签、图像和按钮。 我的问题是当我禁用第一个单元格和滚动表视图第 5 个单元格时也禁用

这里是代码

    override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCellWithIdentifier("cell", forIndexPath: indexPath) as! DetailTableViewCell

    cell.preservesSuperviewLayoutMargins = false
    cell.separatorInset = UIEdgeInsetsZero
    cell.layoutMargins = UIEdgeInsetsZero
    print(indexPath.row)
    if indexPath.row == videoName.count
    {

      cell.video_name?.hidden = true
      cell.artist_name?.hidden = true
      cell.img?.hidden = true
      cell.viewer?.hidden = true
      cell.btn_add?.hidden = true
      tableView.separatorStyle = UITableViewCellSeparatorStyle.None

      indicator = UIActivityIndicatorView(activityIndicatorStyle: UIActivityIndicatorViewStyle.Gray)
      indicator.center = cell.contentView.center

      if self.pageToken != ""
      {
      cell.contentView.addSubview(indicator)
      indicator.startAnimating()
      if videoIDArr.count == 25 || videoIDArr.count == 5
      {
         if check == 0
         {
          loadMoreMostPopularVideo()
         }
         else
         {
          loadMorePlaylistVideo()
         }
      }
      }
    }
    else
    {
     indicator.stopAnimating()

     cell.video_name?.hidden = false
     cell.artist_name?.hidden = false
     cell.img?.hidden = false
     cell.viewer?.hidden = false
     cell.btn_add?.hidden = false
     tableView.separatorStyle = UITableViewCellSeparatorStyle.SingleLine


     cell.video_name?.text = videoName[indexPath.row]
     cell.artist_name?.text = artistName[indexPath.row]

     cell.img?.sd_setImageWithURL(NSURL(string: self.thumbURL[indexPath.row]))
     cell.viewer?.text = "\(viewerArr[indexPath.row]) views"
     cell.btn_add.tag = indexPath.row
     cell.btn_add?.addTarget(self, action: "addVideo:", forControlEvents: UIControlEvents.TouchUpInside)

     // Disable cell code
     if dataVideoName.contains(cell.video_name.text!)
     {
        cell.img.alpha = 0.50
        cell.btn_add.alpha = 0.50
        cell.viewer.alpha = 0.50
        cell.video_name.alpha = 0.50
        cell.artist_name.alpha = 0.50
        cell.userInteractionEnabled = false
     }
    }

    return cell
}

我早上遇到了这个问题 给我一些解决这个问题的建议

谢谢

【问题讨论】:

  • 视频名称是否唯一?另外我建议您需要为每个对象(视频)添加一个标志,该标志将说明单元格是启用还是禁用。并为该检查设置一个 if 条件。
  • 我还添加了我的问题的屏幕截图
  • 所以我建议将该标志添加到您的对象(视频)中,这样您就可以在检查该标志时启用或禁用单元格。
  • 感谢您的建议问题已解决

标签: objective-c swift uitableview


【解决方案1】:

您需要为 if indexPath.row == videoName.count {} 启用单元格代码,以使单元格的设置对称


if indexPath.row == videoName.count {
     ....

     // Enable cell code
     if dataVideoName.contains(cell.video_name.text!)
     {
        cell.img.alpha = 1
        cell.btn_add.alpha = 1
        cell.viewer.alpha = 1
        cell.video_name.alpha = 1
        cell.artist_name.alpha = 1
        cell.userInteractionEnabled = true
     }
} else {
     ....
     // Disable cell code
     if dataVideoName.contains(cell.video_name.text!)
     {
        cell.img.alpha = 0.50
        cell.btn_add.alpha = 0.50
        cell.viewer.alpha = 0.50
        cell.video_name.alpha = 0.50
        cell.artist_name.alpha = 0.50
        cell.userInteractionEnabled = false
     }
}

【讨论】:

  • 我不想启用单元格
  • 我的问题是当第一个单元格被禁用然后我滚动 tableview 然后第 5 个单元格也禁用了单元格的重用(如果有解决方案)
  • @John, tableView.dequeueReusableCellWithIdentifier 表示您缓存了单元格并让它们可重用。因此,您需要始终根据实际情况设置单元格的属性,因为单元格可能会从缓存的单元格中重复使用。
  • 我同意你的观点,但你有什么解决方案可以设置包含数据库值的单元格禁用
  • @John 可能我没有很好地表达我的想法。请检查修改后的答案。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2015-11-13
  • 2013-04-07
  • 1970-01-01
  • 2020-06-03
  • 1970-01-01
  • 2021-05-01
  • 1970-01-01
相关资源
最近更新 更多