简体   繁体   English

swift indexPath.row == 0对于UITableViewCell很奇怪

[英]swift indexPath.row==0 works weirdly for UITableViewCell

I am trying to make a table wherein first cell has a differernt layout than the rest. 我正在尝试制作一张桌子,其中第一个单元格的布局与其余单元格不同。 I want to put the image as background for first cell ie it shd look something like this: 我想将图像作为第一个单元格的背景,即它看起来像这样:

在此处输入图片说明

and here is the code for my implementation 这是我的实现代码

 func imageCellAtIndexPath(indexPath:NSIndexPath) -> MainTableViewCell {
    let cell = self.tableView.dequeueReusableCellWithIdentifier(imageCellIdentifier) as MainTableViewCell
    let object = self.fetchedResultsController.objectAtIndexPath(indexPath) as NSManagedObject

    let eTitle:NSString = object.valueForKey("title")!.description
    let deTitle  = eTitle.stringByDecodingHTMLEntities()
cell.artTitle.text = deTitle


    var full_url = object.valueForKey("thumbnailURL")!.description
    var url = NSURL(string: full_url)
    var image: UIImage?
    var request: NSURLRequest = NSURLRequest(URL: url!)
    NSURLConnection.sendAsynchronousRequest(request, queue: NSOperationQueue.mainQueue(), completionHandler: {(response: NSURLResponse!, data: NSData!, error: NSError!) -> Void in

        image = UIImage(data: data)
        if((indexPath.row)==0)  {
        var imageView = UIImageView(frame: CGRectMake(10, 10, cell.frame.width - 10, cell.frame.height - 10))
        imageView.image = image
       cell.backgroundView = UIView()
      cell.backgroundView?.addSubview(imageView)

        }
        else{
        cell.thumb.image = image
        }
    })

    return cell
}

bt the problem is.. when i scroll down and scroll up back again, the background image starts repeating and the thumbnails also get overlapped as shown: bt问题是..当我向下滚动并再次向上滚动时,背景图像开始重复,并且缩略图也重叠,如下所示: 在此处输入图片说明

If i scroll up and down again.. this is wht happens: 如果我再次上下滚动..这是发生了什么: 在此处输入图片说明

i might have done some silly mistake bt m not able to figure out what it is. 我可能做了一些愚蠢的错误bt m,无法弄清楚它是什么。 pls help 请帮助

The problem is that the cell is being reused by the tableView for efficiency purposes but it is never being reset. 问题是tableView出于效率目的正在重用该单元格,但从未对其进行重置。

You need to clear / remove the imageView from the background view if the cell is not at indexPath 0. 如果单元格不在indexPath 0,则需要从背景视图中清除/删除imageView。

In table views cells are reused and you should reset parts of the cell that are not relevant the specific version of the cell style you are after. 在表格视图中,单元格被重用您应重置 单元格中 使用的单元格样式的特定版本无关的部分。 Something like: 就像是:

 if((indexPath.row)==0)  {
      let frame = CGRectMake(10, 10, 
                             cell.frame.width - 10, cell.frame.height - 10)
      var imageView = UIImageView(frame: frame)
      imageView.image = image
      cell.backgroundView = UIView()
      cell.backgroundView?.addSubview(imageView)

      // Reset 
      cell.thumb.image = nil
 } else{
      cell.thumb.image = image
      // Reset 
      cell.backgroundView = nil
 }

Even better and more idiomatic idea is to use separate UITableViewCell designs for these two cell types, each with different reuse identifiers. 更好,更惯用的想法是针对这两种单元格类型使用单独的UITableViewCell设计,每种类型具有不同的重用标识符。 This way you don't need to care about resetting. 这样,您无需担心重置。

PS You should use dequeueReusableCellWithIdentifier:forIndexPath: instead of older dequeueReusableCellWithIdentifier: as it guarantees that a cell is returned. PS:您应该使用dequeueReusableCellWithIdentifier:forIndexPath:而不是较早的dequeueReusableCellWithIdentifier:因为它可以确保返回单元格。

Maybe you can use tableHeaderView for this? 也许您可以为此使用tableHeaderView Or, if you want a lot sections like this, you probably can use 或者,如果您想要很多这样的部分,则可以使用

func headerViewForSection(_ section: Int) -> UITableViewHeaderFooterView?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM