我有一个表格视图与自定义tableview单元格。 每个可能有不同的高度。 总共有14行,但其他行不可见,当我上下滚动行正在消失。tableview与自定义表格视图单元格在滚动消失
请建议我在哪里做错了。这是从2天开始纠缠我的。我无法找到任何解决方案。
请在下面找到我的代码为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
}
这种情况滚动后向下和向上
我刚刚删除它们以使代码但我实际上在我的代码中有它们,但仍然发生同样的情况,我试着改变默认类型,文本类型和所有其他类型的高度,但没有任何反应,但你的假设是正确的。设置了ta使用原型单元的可视化。最初,当我向下滚动时,代码只加载14行中的10行,无法创建其他4行。我看到cellForRowAt indexPath在滚动上再次执行,但它不会创建单元格。 – Dev
@NewbieIOS我看到了..所以,当你向上和向下滚动时,你是否看到很多“虚拟单元格”在控制台中打印出来? – user3608914
我预见到问题可能不在于switch语句。所以,它会传播到变量variableType = fetchedElements [indexPath.row] .elementType if(variableType == nil) { variableType =“”; } – user3608914