swift - 带有自定义表格 View 单元格的表格 View 在滚动时消失

标签 swift uitableview swift3 tableview

我有一个带有自定义表格 View 单元格的表格 View 。 每个可能有不同的高度。 总共有 14 行,但其他行不可见,当我上下滚动时,这些行正在消失。

请指出我哪里做错了。这困扰了我 2 天。我找不到任何解决方案。

请在下面找到我的表格 View 代码。

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

}

enter image description here

这发生在向下和向上滚动之后

enter image description here

最佳答案

注意:我无法发表评论,因为我没有足够的声誉,所以我无法就我遇到的某些问题询问您。但是通过阅读您的代码,这就是我可以提出的建议。

我可以看到你的数据有相当多的元素类型,例如,Picklist,divider,scanner 等,在你的 heightForRow Delegate 方法中..

但是,在您的 cellForRow 函数中,您只有两种情况,PickList 和 MultiSelect...因此,所有具有不同元素类型的数据类型都会返回一个带有大“默认值”的单元格。

我唯一不明白的是,tableview 似乎在第一次尝试时就完美加载了。因此,我想知道您是如何设置 tableview 的。

如果您在 Storyboard中手动设置它。那么第一次,当应用程序加载时,它会显示您在 UITableView 中设计的任何内容,但是当您上下滚动时,CellForItem 方法将启动,并重新编写单元格,返回您在列表中看到的大“默认”单元格。

所以如果我猜对了..

然后您要做的就是简单地将所有类型案例添加到您的 cellForRow 方法的 switch 语句中。

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

相关文章:

ios - 在没有 Init() 的情况下创建 UIView 子类 - Swift?

iphone - 如何在不重新加载完整表格的情况下更新 UITableVIewCell

ios - swift 3.0 的 map View 注释委托(delegate)

ios - Swift 3 附加到 UIImageView

ios - UILocalNotification 的 UserInfo 始终为 nil

ios - 编辑tableView时自定义单元格imageView移动到左侧

ios - bash ./Submodules/OTRKit/scripts/build-all.sh 配置 : error: C compiler cannot create executables

ios - CI - Xcode 9 服务器在集成后不发送电子邮件

iphone - cellForRowAtIndexPath 中的 ReusableCellWithIdentifier 问题

ios - Swift 中静态 TableView 中特定单元格的公开指示器