Scrollview内部的UITableview调用cellforrow太多次了

时间:2017-03-24 07:46:58

标签: ios uitableview swift3

我在scrollview中有一个tableview,我有1000行。 然后app调用cellForRowAtIndexPath 1000次,即使设备中没有全部可见。它使我的应用程序真的很慢。

那么如何解决这个问题呢?

提前致谢!

enter image description here

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    // tableView.tag >= 80000 為popTable
    if tableView.tag < k79995 {
        var cell: BookmarkContentCell!
        if indexPath.row > 0 && indexPath.row < channelArray.count - 1 {
            cell = tableView.dequeueReusableCell(withIdentifier: middleReuseIdentifier, for: indexPath) as! BookmarkContentCell
        } else if indexPath.row == 0 {
            cell = tableView.dequeueReusableCell(withIdentifier: firstReuseIdentifier, for: indexPath) as! BookmarkContentCell
            cell.cellPosition = .first
        } else if indexPath.row == channelArray.count - 1 {
            cell = tableView.dequeueReusableCell(withIdentifier: lastReuseIdentifier, for: indexPath) as! BookmarkContentCell
        }
        cell.musicTitleLabel.backgroundColor = UIColor.white
        cell.musicTitleLabel.text = channelArray[indexPath.row].name
        cell.musicTitleLabel.textColor = FONT_COLOR_2
        cell.moreBtn.tag = indexPath.row
        cell.moreBtn.removeTarget(nil, action: nil, for: .allEvents)
        cell.moreBtn.addTarget(self, action: #selector(moreBtnPressed(_:)), for: .touchUpInside)
        let imageURL = channelArray[indexPath.row].thumbnailURL

        cell.loadCoverImage(imageURL: imageURL)

        cell.tag = indexPath.row
        addCellBackground(cell, row: indexPath.row, count: channelArray.count)
        cell.separatorInset = UIEdgeInsets(top: 0, left: 2, bottom: 0, right: 2)
        cell.coverImageView.isOpaque = true
        cell.moreBtn.isOpaque = true
        cell.musicTitleLabel.isOpaque = true
        counter += 1
        DLog("cellforrow::indexPath.row:: \(indexPath.row)")
        return cell
    } else {    // 彈出對話框
        var cell = tableView.dequeueReusableCell(withIdentifier: POP_TABLE_VIEW_CELL_IDENTIFY)
        if cell == nil {
            let nib = UINib(nibName: POP_TABLE_VIEW_CELL_XIB_NAME, bundle: nil)
            tableView.register(nib, forCellReuseIdentifier: POP_TABLE_VIEW_CELL_IDENTIFY)
            cell = tableView.dequeueReusableCell(withIdentifier: POP_TABLE_VIEW_CELL_IDENTIFY)
        }
        cell!.textLabel?.text = popBoxTitles[indexPath.row]
        cell!.separatorInset = UIEdgeInsets(top: 0, left: 2, bottom: 0, right: 2)
        return cell!
    }
}

1 个答案:

答案 0 :(得分:0)

这是我的愚蠢。下面的代码会导致问题。

tableHeightConstraint.constant = tableView.contentSize.height