为什么我的饲料细胞会重复?

时间:2016-02-21 06:55:35

标签: ios xcode swift

我正在使用此代码创建一个Feed视图,该视图显示类似于instagram的用户,图片和评论。出于某种原因,Feed上的单元格会复制当前用户的帖子。不仅如此,它还将不正确的用户名与重复单元格上的图像放在一起。我做错了什么?

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>


<input type="text" id="search-text" placeholder="search">
<ul id="list">
  <li class="searchArray">Apple pie</li>
  <li class="searchArray">Pumpkin pie</li>
  <li class="searchArray">Banana-creme pie</li>
  <li class="searchArray">Peach-blackberry cobbler</li>
  <li class="searchArray">Chocolate-strawberry torte</li>
  <li class="searchArray">Chocolate-zucchini cake</li>
  <li class="searchArray">Anything involving chocolate and mint</li>
  <li class="searchArray">Red-velvet cake</li>
  <li class="searchArray">Anything involving fruits that aren't cherries</li>
</ul>

2 个答案:

答案 0 :(得分:1)

显然,生成重复的内容是因为你已经设置if imageFiles.count > 0的条件,然后显示数据。

但是什么时候没有图像?它肯定会从可重用UITableViewCell中获取价值。检查以下更改:

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

        if imageFiles.count > 0{
            myCell.userLabel.text = "\(usernames[indexPath.row]) completed the \(imageComment[indexPath.row]) challenge!"
            imageFiles[indexPath.row].getDataInBackgroundWithBlock({ (data, error) -> Void in
                if let downloadedImage = UIImage(data: data!) {

                    myCell.imagePost.image = downloadedImage
//                    self.tableView.reloadData()

                }
            })
        }else{

             myCell.userLabel.text = "Put What You Want Here" //make just nil 
             myCell.imagePost.image = UIImage(name: "placeholder.png") //Some Placeholder image when there is no data
        }
        return myCell
    }

答案 1 :(得分:1)

您应该在添加新值之前重置单元格属性,您可以使用

prepareForReuse()

有关Apple Doc https://developer.apple.com/library/prerelease/ios/documentation/UIKit/Reference/UITableViewCell_Class/index.html#//apple_ref/occ/instm/UITableViewCell/prepareForReuse

的更多信息