我有一个带有自定义TableView单元格的表视图。
每个都有不同的高度。
总共有14行,但其他行不可见,当我上下滚动时,这些行正在消失。
请告诉我哪里做错了。这让我从两天就烦透了。我找不到任何解决办法。
请在下面找到TableView的代码。

func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat
{
    return UITableViewAutomaticDimension

}
func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
    if(fetchedElements[indexPath.row].elementType=="textarea")
    {
        return 100 ;
    }
    else if (fetchedElements[indexPath.row].elementType=="picklist")
    {
        return 60;
    }
    else if (fetchedElements[indexPath.row].elementType=="divider")
    {
        return 30;
    }
    else if (fetchedElements[indexPath.row].elementType=="scanner")
    {
        return 50;
    }
    else if (fetchedElements[indexPath.row].elementType=="multiselect")
    {
        return 60;
    }
    else if(fetchedElements[indexPath.row].elementType=="text")
    {
       var length =  fetchedElements[indexPath.row].elementText?.characters.count
        if length! < 30
        {
            return 80;
        }else if length! < 60 && length! > 30  {
            return 110;
        }else if(length! < 100 && length! > 60)
        {
            return 130;
        }else if(length! < 150 && length! > 100 )
        {
        return 170;
        }
        else{
            return 140;
        }
    }else
   {
        return 200;

    }

}


func tableView(tableView: UITableView, estimatedHeightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat
{
    return UITableViewAutomaticDimension

}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {

    print("No of elements Fetched===\(fetchedElements.count)")
    return fetchedElements.count
}

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {


var variableType =  fetchedElements[indexPath.row].elementType
    if(variableType==nil)
    {
      variableType = "";
    }

var elementType = variableType!

    print("=============Element Type=\(elementType)==============")

   let frame = UIScreen.main.bounds

    switch elementType {
    case "picklist":

        let dpcell = Bundle.main.loadNibNamed("Textbox", owner: self, options: nil)?.first as! Textbox

        dpcell.backgroundColor = UIColor.lightGray
        dpcell.txtValue.layer.cornerRadius = 3


        dpcell.LabelName.text = "Country"//fetchedElements[indexPath.row].elementText
        dpcell.txtValue.tag =  Int(fetchedElements[indexPath.row].elementId)
        dpcell.txtValue.addTarget(self, action: #selector(GoToDropdown), for: UIControlEvents.touchDown)
        dpcell.txtValue.backgroundColor = UIColor.white
        dpcell.txtValue.titleLabel?.numberOfLines = 0
        dpcell.txtValue.titleLabel?.adjustsFontSizeToFitWidth = true;
        dpcell.LabelName.lineBreakMode = .byWordWrapping
        dpcell.LabelName.numberOfLines = 0
        dpcell.selectionStyle = .none
        print("============picklist==========\(indexPath.row)")
        return dpcell
    case "multiselect":
        let dpcell = Bundle.main.loadNibNamed("Textbox", owner: self, options: nil)?.first as! Textbox

        dpcell.backgroundColor = UIColor.lightGray
        dpcell.txtValue.layer.cornerRadius = 3
        dpcell.LabelName.text = fetchedElements[indexPath.row].elementText
        dpcell.txtValue.tag =  Int(fetchedElements[indexPath.row].elementId)
        dpcell.txtValue.addTarget(self, action: #selector(GoToMultiSelect), for: UIControlEvents.touchDown)
        dpcell.txtValue.backgroundColor = UIColor.white
        dpcell.txtValue.contentHorizontalAlignment = UIControlContentHorizontalAlignment.left
        dpcell.txtValue.titleLabel?.numberOfLines = 0
        dpcell.txtValue.titleLabel?.adjustsFontSizeToFitWidth = true;
        dpcell.selectionStyle = .none
         print("===========multiselect===========\(indexPath.row)")

        return dpcell

     default:
        let cell = self.tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath) as! UITableViewCell
        cell.textLabel?.text = "default"
         print("==========dummy cell============\(indexPath.row)")
        return cell

}

swift - 自定义表 View 单元格的tableview在滚动时消失-LMLPHP
上下滚动后会发生这种情况
swift - 自定义表 View 单元格的tableview在滚动时消失-LMLPHP

最佳答案

注:我不能评论,因为我没有足够的声誉,所以我不能问你关于我的某些问题。但通过阅读你的密码,这就是我的建议。
我可以看到,在heightforrow委托方法中,数据有很多元素类型,例如picklist、divider、scanner和ect。
但是,在cellforrow函数中,只有两种情况,即picklist和multiselect…因此,所有具有不同元素类型的数据类型都将返回一个带有大“默认值”的单元格。
唯一我不明白的是TableView在第一次尝试时似乎已经完全加载了。因此,我想知道您是如何设置TableView的。
如果您手动在故事板中设置它…然后,第一次,当应用程序加载时,它将呈现您在uiTableView中设计的任何内容,但当您上下滚动时,cellForItem方法将启动并重新编写该单元格,返回您在列表中看到的大“默认”单元格。
所以如果我猜对了…
然后,只需在cellforrow方法的switch语句中添加所有类型的大小写即可。

关于swift - 自定义表 View 单元格的tableview在滚动时消失,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/40961200/

10-11 22:32
查看更多