Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/swift/18.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Swift 如何在调用cellforRowAt之前清除tableview单元格?_Swift_Tableview - Fatal编程技术网

Swift 如何在调用cellforRowAt之前清除tableview单元格?

Swift 如何在调用cellforRowAt之前清除tableview单元格?,swift,tableview,Swift,Tableview,我有以下代码 override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell{ //creating a cell using the custom class if (indexPath.row == 0 && indexPath.section == 0) {

我有以下代码

    override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell{
        //creating a cell using the custom class
        if (indexPath.row == 0 && indexPath.section == 0) {


            var cell:UITableViewCell;
            cell = tableView.dequeueReusableCell(withIdentifier: "book", for: indexPath)
            cell.selectionStyle = UITableViewCell.SelectionStyle.none;


            self.ref.observe(DataEventType.value, with: { (snapshot) in
                self.ref.child("featured").observeSingleEvent(of: .value, with: { (snapshot) in
                    // Get user value
                    let value = snapshot.value as? NSDictionary
                    let title = value?["title"] as? String ?? ""
                    self.featured = value?["url"] as? String ?? ""

                    let label = UILabel(frame: CGRect(x: 0, y: 0, width: cell.frame.size.width, height: 24))
                    label.numberOfLines = 0
                    label.font = UIFont(name: "CeraPro-Medium", size: 20);
                    label.textColor = UIColor.black
                    label.text = NSLocalizedString("   Free Daily Pick", comment: "featured")
                    label.adjustsFontForContentSizeCategory = true
                    cell.addSubview(label)

                    if let image = value?["image"] as? String, image.isEmpty {
                        print("String is nil or empty.")
                    } else {
                        if let image = value?["image"]  {

                            let imageView = UIImageView();
                            imageView.frame = CGRect(x: 0,
                                                     y: 75,
                                                     width: cell.frame.size.width,
                                                     height: 150)
                            imageView.contentMode = .scaleAspectFill
                            imageView.isUserInteractionEnabled = false

                            imageView.sd_setImage(with: URL(string: image as! String), placeholderImage: nil)
                            cell.addSubview(imageView)

                            let label = UILabel(frame: CGRect(x: 10,
                                                              y: cell.frame.size.height-60,
                                                              width: cell.frame.size.width,
                                                              height: 50));
                            label.textColor = UIColor.black
                            label.backgroundColor = UIColor.init(red: 1, green: 1, blue: 1, alpha: 0.5)
                            label.font = UIFont(name: "CeraPro-Bold", size: 16)
                            label.text = "  \(title)"

                            let view = UIView()
                            view.frame = CGRect(x: 0, y: 0, width: cell.frame.size.width, height: 150);
                            view.tag = 123
                            view.addSubview(imageView)
                            view.addSubview(label)
                            cell.contentView.addSubview(view)
                        }
                    }
                    // ...
                }) { (error) in
                    print(error.localizedDescription)
                }

            })

            return cell
        } else {
            let cell = tableView.dequeueReusableCell(withIdentifier: "book", for: indexPath) as! BookTableViewCell

            cell.cellIndex = indexPath
            cell.dataSource = self
            cell.delegate = self
            cell.backgroundColor = UIColor.white
            var books = [Book]();

            let count = self.enterpriseBooks.count;
            if count > 0 && indexPath.section <= self.enterpriseBooks_2.count {
                books = self.enterpriseBooks_2[indexPath.section - 1]!;
            }

            if (indexPath.section == (count + 1)) {
                books = nytbooks;
            } else if (indexPath.section == (count + 2)) {
                books = trendingbooks;
            } else if (indexPath.section == (count + 3)) {
                books = newbooks
            }

            if (books.count > 0) {
                if (cell.collectionView === nil) {
                    cell.addCollectionView();
                    cell.collectionView.reloadData()
                }
            }
            return cell
        }
}
override func tableView(tableView:UITableView,cellForRowAt indexath:indexPath)->UITableViewCell{
//使用自定义类创建单元格
if(indexPath.row==0&&indexPath.section==0){
变量单元格:UITableViewCell;
cell=tableView.dequeueReusableCell(带有标识符:“book”,for:indexath)
cell.selectionStyle=UITableViewCell.selectionStyle.none;
self.ref.observe(DataEventType.value),在
self.ref.child(“特色”).observeSingleEvent(of:.值,其中:{(快照)位于
//获取用户值
设value=snapshot.value为?NSDictionary
让title=值?[“title”]作为字符串??“
self.featured=值?[“url”]作为?字符串??“
let label=UILabel(帧:CGRect(x:0,y:0,宽度:cell.frame.size.width,高度:24))
label.numberOfLines=0
label.font=UIFont(名称:“CeraPro中等”,大小:20);
label.textColor=UIColor.black
label.text=NSLocalizedString(“免费每日选择”,注释:“精选”)
label.adjustsFontForContentSizeCategory=true
cell.addSubview(标签)
如果让image=value?[“image”]作为?字符串,则image.isEmpty{
打印(“字符串为零或空”)
}否则{
如果让图像=值?[“图像”]{
让imageView=UIImageView();
imageView.frame=CGRect(x:0,
y:75,
宽度:cell.frame.size.width,
身高:150)
imageView.contentMode=.ScaleSpectFill
imageView.isUserInteractionEnabled=false
imageView.sd_setImage(带:URL(字符串:图像为!字符串),占位符图像:nil)
cell.addSubview(imageView)
让label=UILabel(帧:CGRect(x:10,
y:cell.frame.size.height-60,
宽度:cell.frame.size.width,
身高:50),;
label.textColor=UIColor.black
label.backgroundColor=UIColor.init(红色:1,绿色:1,蓝色:1,阿尔法:0.5)
label.font=UIFont(名称:“CeraPro Bold”,大小:16)
label.text=“\(标题)”
let view=UIView()
view.frame=CGRect(x:0,y:0,宽度:cell.frame.size.width,高度:150);
view.tag=123
view.addSubview(imageView)
view.addSubview(标签)
cell.contentView.addSubview(视图)
}
}
// ...
}){(错误)在
打印(错误。本地化描述)
}
})
返回单元
}否则{
让cell=tableView.dequeueReusableCell(标识符为“book”,for:indexath)作为!BookTableViewCell
cell.cellIndex=indexath
cell.dataSource=self
cell.delegate=self
cell.backgroundColor=UIColor.white
var books=[Book]();
让计数=self.enterpriseBooks.count;
如果计数>0&&indexath.0节){
if(cell.collectionView==nil){
cell.addCollectionView();
cell.collectionView.reloadData()
}
}
返回单元
}
}
这里是第0节第0行-我展示了一幅特色图片 对于第1、2、3节,我展示了书籍图像的水平收藏视图


现在的情况是,通过使用dequeue函数获取表视图单元格,表视图正在缓存一些相互重叠的表视图单元格内容。在调用cellForRowAt之前,是否有方法清除表视图单元格?

好的,要处理缓存数据,您必须处理所有其他情况,以便在无/失败情况下为UI元素分配适当的值。对于一个实例,如果图像url像这样为nil/空,则必须将nil分配给UIImageView的图像属性

    override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell{
    //creating a cell using the custom class
    if (indexPath.row == 0 && indexPath.section == 0) {


        var cell:UITableViewCell;
        cell = tableView.dequeueReusableCell(withIdentifier: "book", for: indexPath)
        cell.selectionStyle = UITableViewCell.SelectionStyle.none;


        self.ref.observe(DataEventType.value, with: { (snapshot) in
            self.ref.child("featured").observeSingleEvent(of: .value, with: { (snapshot) in
                // Get user value
                let value = snapshot.value as? NSDictionary
                let title = value?["title"] as? String ?? ""
                self.featured = value?["url"] as? String ?? ""

                let label = UILabel(frame: CGRect(x: 0, y: 0, width: cell.frame.size.width, height: 24))
                label.numberOfLines = 0
                label.font = UIFont(name: "CeraPro-Medium", size: 20);
                label.textColor = UIColor.black
                label.text = NSLocalizedString("   Free Daily Pick", comment: "featured")
                label.adjustsFontForContentSizeCategory = true
                cell.addSubview(label)
                let imageView = UIImageView();

                if let image = value?["image"] as? String, image.isEmpty {
                    print("String is nil or empty.")
                    imageView.image = nil
                    // same goes for the label
                } else {
                    if let image = value?["image"]  {

                        imageView.frame = CGRect(x: 0,
                                                 y: 75,
                                                 width: cell.frame.size.width,
                                                 height: 150)
                        imageView.contentMode = .scaleAspectFill
                        imageView.isUserInteractionEnabled = false

                        imageView.sd_setImage(with: URL(string: image as! String), placeholderImage: nil)
                        cell.addSubview(imageView)

                        let label = UILabel(frame: CGRect(x: 10,
                                                          y: cell.frame.size.height-60,
                                                          width: cell.frame.size.width,
                                                          height: 50));
                        label.textColor = UIColor.black
                        label.backgroundColor = UIColor.init(red: 1, green: 1, blue: 1, alpha: 0.5)
                        label.font = UIFont(name: "CeraPro-Bold", size: 16)
                        label.text = "  \(title)"

                        let view = UIView()
                        view.frame = CGRect(x: 0, y: 0, width: cell.frame.size.width, height: 150);
                        view.tag = 123
                        view.addSubview(imageView)
                        view.addSubview(label)
                        cell.contentView.addSubview(view)
                    }
                }
                // ...
            }) { (error) in
                print(error.localizedDescription)
            }

        })

        return cell
    } else {
        let cell = tableView.dequeueReusableCell(withIdentifier: "book", for: indexPath) as! BookTableViewCell

        cell.cellIndex = indexPath
        cell.dataSource = self
        cell.delegate = self
        cell.backgroundColor = UIColor.white
        var books = [Book]();

        let count = self.enterpriseBooks.count;
        if count > 0 && indexPath.section <= self.enterpriseBooks_2.count {
            books = self.enterpriseBooks_2[indexPath.section - 1]!;
        }

        if (indexPath.section == (count + 1)) {
            books = nytbooks;
        } else if (indexPath.section == (count + 2)) {
            books = trendingbooks;
        } else if (indexPath.section == (count + 3)) {
            books = newbooks
        }

        if (books.count > 0) {
            if (cell.collectionView === nil) {
                cell.addCollectionView();
                cell.collectionView.reloadData()
            }
        }
        return cell
    }
override func tableView(tableView:UITableView,cellForRowAt indexath:indexPath)->UITableViewCell{
//使用自定义类创建单元格
if(indexPath.row==0&&indexPath.section==0){
变量单元格:UITableViewCell;
cell=tableView.dequeueReusableCell(带有标识符:“book”,for:indexath)
cell.selectionStyle=UITableViewCell.selectionStyle.none;
self.ref.observe(DataEventType.value),在
self.ref.child(“特色”).observeSingleEvent(of:.值,其中:{(快照)位于
//获取用户值
设value=snapshot.value为?NSDictionary
让title=值?[“title”]作为字符串??“
self.featured=值?[“url”]作为?字符串??“
let label=UILabel(帧:CGRect(x:0,y:0,宽度:cell.frame.size.width,高度:24))
label.numberOfLines=0
label.font=UIFont(名称:“CeraPro中等”,大小:20);
label.textColor=UIColor.black
label.text=NSLocalizedString(“免费每日选择”,注释:“精选”)
label.adjustsFontForContentSizeCategory=true
cell.addSubview(标签)
让我想象一下