滚动视图中的 UITableview 调用 cellforrow 太多次 [英] UITableview inside Scrollview call cellforrow too many times

查看:25
本文介绍了滚动视图中的 UITableview 调用 cellforrow 太多次的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在滚动视图中有一个表视图,并且有 1000 行.然后应用程序调用 cellForRowAtIndexPath 1000 次,即使在设备中也不是所有可见的.它让我的应用程序变得很慢.

I have a tableview inside a scrollview, and I have 1000 rows. Then app call cellForRowAtIndexPath 1000 times, even there are not all visible in device. It make my app really slow.

那么如何解决这个问题?

So how to solve this problem?

提前致谢!

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

推荐答案

这是我的笨蛋.下面的代码导致了问题.

It's my stupid。The belowing code cause the problem.

tableHeightConstraint.constant = tableView.contentSize.height

这篇关于滚动视图中的 UITableview 调用 cellforrow 太多次的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆