可重用单元格问题迅速

可重用单元格问题迅速

本文介绍了UITableview 可重用单元格问题迅速的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

这是代码

 override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) ->UITableViewCell {let cell = tableView.dequeueReusableCellWithIdentifier("cell", forIndexPath: indexPath) as!细节表视图单元格cell.preservesSuperviewLayoutMargins = falsecell.separatorInset = UIEdgeInsetsZerocell.layoutMargins = UIEdgeInsetsZero打印(indexPath.row)如果 indexPath.row == videoName.count{cell.video_name?.hidden = truecell.artist_name?.hidden = truecell.img?.hidden = truecell.viewer?.hidden = truecell.btn_add?.hidden = truetableView.separatorStyle = UITableViewCellSeparatorStyle.None指标 = UIActivityIndi​​catorView(activityIndi​​catorStyle: UIActivityIndi​​catorViewStyle.Gray)indicator.center = cell.contentView.center如果 self.pageToken != ""{cell.contentView.addSubview(指标)指标.startAnimating()如果 videoIDArr.count == 25 ||videoIDArr.count == 5{如果检查 == 0{loadMoreMostPopularVideo()}别的{加载更多播放列表视频()}}}}别的{指标.stopAnimating()cell.video_name?.hidden = falsecell.artist_name?.hidden = falsecell.img?.hidden = falsecell.viewer?.hidden = falsecell.btn_add?.hidden = falsetableView.separatorStyle = UITableViewCellSeparatorStyle.SingleLinecell.video_name?.text = videoName[indexPath.row]cell.artist_name?.text = 艺术家名[indexPath.row]cell.img?.sd_setImageWithURL(NSURL(string: self.thumbURL[indexPath.row]))cell.viewer?.text = "\(viewerArr[indexPath.row]) 视图"cell.btn_add.tag = indexPath.rowcell.btn_add?.addTarget(self, action: "addVideo:", forControlEvents: UIControlEvents.TouchUpInside)//禁用单元格代码如果 dataVideoName.contains(cell.video_name.text!){cell.img.alpha = 0.50cell.btn_add.alpha = 0.50cell.viewer.alpha = 0.50cell.video_name.alpha = 0.50cell.artist_name.alpha = 0.50cell.userInteractionEnabled = false}}返回单元格}

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

谢谢

解决方案

if indexPath.row == videoName.count {} 需要启用单元格代码才能使单元格设置对称

如果 indexPath.row == videoName.count {....//启用单元格代码如果 dataVideoName.contains(cell.video_name.text!){cell.img.alpha = 1cell.btn_add.alpha = 1cell.viewer.alpha = 1cell.video_name.alpha = 1cell.artist_name.alpha = 1cell.userInteractionEnabled = true}} 别的 {....//禁用单元格代码如果 dataVideoName.contains(cell.video_name.text!){cell.img.alpha = 0.50cell.btn_add.alpha = 0.50cell.viewer.alpha = 0.50cell.video_name.alpha = 0.50cell.artist_name.alpha = 0.50cell.userInteractionEnabled = false}}

I have tableview in which each cell contains label,image and button.My problem is when i disabled first cell and scroll table view 5th cell also disable

here is code

    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
}

I have face this problem in morninggive me some suggestion to solve this problem

Thanks

解决方案

You need enable cell codes for if indexPath.row == videoName.count {} to have cell's settings symmetrically


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
     }
}

这篇关于UITableview 可重用单元格问题迅速的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-24 02:52