单个UITableView具有不同的自定义UITableViewCells问题

时间:2016-07-30 14:38:25

标签: ios uitableview uiscrollview reloaddata

我已将UITableView添加到UIScrollView中,我已经为UITableView的高度约束创建了一个IBOutlet,这有助于我设置UITableview的内容大小。

我有3个标签,我切换标签以使用不同的数据源重新加载数据。此外,当标签更改时,我有不同的自定义单元格。 因此,当标签更改时,我调用reloadData

这是我的cellForRow函数

 func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {

        // Configure the cell...

        var cell:UITableViewCell!

        let event:Event!


        if(tableView == self.dataTableView)
        { 

                let eventCell:EventTableViewCell = tableView.dequeueReusableCellWithIdentifier(kCellIdentifier, forIndexPath: indexPath) as! EventTableViewCell

                eventCell.delegate = self


                    event = sectionsArray[indexPath.section].EventItems[indexPath.row]




                eventCell.eventTitleLabel?.text = "\(event.title)"
                eventCell.eventImageView?.image = UIImage(named: "def.png")


                if let img = imageCache[event.imgUrl] {
                    eventCell.eventImageView?.image = img
                }
                else {
                    print("calling image of \(indexPath.row)  \(event.imgUrl)")
                    //      let escapedString = event.imgUrl.stringByAddingPercentEncodingWithAllowedCharacters(.URLHostAllowedCharacterSet())
                    let session = NSURLSession.sharedSession()
                    do {
                        let encodedImageUrl = CommonEHUtils.urlEncodeString(event.imgUrl)
                        let urlObj = NSURL(string:encodedImageUrl)
                        if urlObj != nil {
                            let task = session.dataTaskWithURL(urlObj!, completionHandler: { ( data: NSData?, response: NSURLResponse?, error: NSError?) -> Void in
                                guard let realResponse = response as? NSHTTPURLResponse where
                                    realResponse.statusCode == 200 else {
                                        print("Not a 200 response, url = " + event.imgUrl)
                                        return
                                }
                                if error == nil {
                                    // Convert the downloaded data in to a UIImage object
                                    let image = UIImage(data: data!)


                                    // Store the image in to our cache
                                    self.imageCache[event.imgUrl] = image
                                    // Update the cell
                                    dispatch_async(dispatch_get_main_queue(), {
                                        if let cellToUpdate:EventTableViewCell = tableView.cellForRowAtIndexPath(indexPath) as? EventTableViewCell  {
                                            cellToUpdate.eventImageView?.image = image
                                        }
                                    })
                                }
                            })
                            task.resume()
                        }
                    } catch {
                        print("Cant fetch image \(event.imgUrl)")
                    }
                }


                cell = eventCell
            }
            else if(secodTabClicked)
            {
                let Cell2:cell2TableViewCell = tableView.dequeueReusableCellWithIdentifier(CellIdentifier1, forIndexPath: indexPath) as! cell2TableViewCell

               //Image loading again takes place here                

                cell = Cell2

            }
else if(thirdTabClicked)
            {
                let Cell3:cell3TableViewCell = tableView.dequeueReusableCellWithIdentifier(CellIdentifier2, forIndexPath: indexPath) as! cell3TableViewCell

               //Image loading again takes place here                

                cell = Cell3

            }


        return cell

    }

正如您所看到的,每个标签都有不同的带图像的自定义单元格。

以下是我面临的问题

1)切换标签时重新加载数据需要时间,而且延迟时间相当长。在iphone 4s上它更糟糕

2)当我打开此页面时,默认选择第一个选项卡,因此当我滚动时,一切都运行顺畅。但是当我切换标签时,当我重新加载数据后再次滚动时,滚动变得不稳定并立即出现内存警告问题。

到目前为止我做了什么?

1)我评论了图像提取代码,并检查是否导致生涩的滚动,但不是。

2)我使用时间分析器来检查花费更多时间的内容,并指出" dequeueReusableCellWithIdentifier"。所以我不知道这里出了什么问题。

1 个答案:

答案 0 :(得分:0)

您的代码看起来不对称"关于secodTabClickedthirdTabClicked时设置的单元格。我没有看到firstTabClicked,并且在我看来,您用来确定点击哪个标签的条件与secodTabClickedthirdTabClicked重叠。换句话说,您可能会进入顶部分支,并在预期EventTableViewCellcell2TableViewCell时返回cell3TableViewCell

重构代码以进行类型选择"对称"关于所有三种细胞类型应该解决这个问题。

另一种解决方案可能是为不同的选项卡创建单独的数据源,并切换数据源而不是设置xyzTabClicked标志。您最终将使用小功能代替一个大功能,这将使您的代码更易于管理。