我正在使用 Rubymotion 构建一个 iOS 应用程序。在这个应用程序中,我得到了一个包含许多行的表格视图。每行都有标签。主要内容标签的高度各不相同。因此,行的高度不同(按设计)。
问题是 dequeueReusableCellWithIdentifier。当我为所有行设置相同的值时,我会遇到重叠行和重复的问题。当我为每一行设置一个唯一值时,它工作正常,但滚动性能不是很好。
处理自定义行(不同高度)的正确方法是什么?
感谢所有输入!
更新
这是我的 heightForRowAtIndexPath:indexPath
def tableView(tableView, heightForRowAtIndexPath:indexPath)
object = @messages.objectAtIndex(indexPath.row)
labelSize = object.message.sizeWithFont("Arial".uifont(14), constrainedToSize:[250.0, Float::MAX], lineBreakMode:UILineBreakModeWordWrap)
labelSize.height + 88
end
更新 2
def tableView(tableView, cellForRowAtIndexPath:indexPath)
# Get row data and create row
data = @messages[indexPath.row]
# Create the label and add to view
main_label = UILabel.new
main_label.frame = [[10, 0],[250, 40]]
main_label.text = data.sender
main_label.font = "Arial".uifont(14)
main_label.textColor = UIColor.blackColor
main_label.backgroundColor = UIColor.clearColor
main_label.tag = 1
# Create the label and add to view
sub_label = UILabel.new
sub_label.frame = [[10, 35],[250, 40]]
sub_label.text = data.message
sub_label.font = "Arial".uifont(14)
sub_label.textColor = UIColor.grayColor
sub_label.backgroundColor = UIColor.clearColor
sub_label.numberOfLines = 0
sub_label.sizeToFit
sub_label.tag = 2
# Create the label and add to view
time_label = UILabel.new
time_label.frame = [[10, 2],[250, 21]]
time_label.text = data.created_at_human
time_label.font = "Arial".uifont(13)
time_label.textColor = BubbleWrap.rgb_color(123, 137, 165)
time_label.backgroundColor = UIColor.clearColor
time_label.tag = 3
# Create the main view
main_view = UIView.alloc.initWithFrame([[10, 10],[300, sub_label.size.height + 50]])
main_view.backgroundColor = UIColor.whiteColor
main_view.addSubview(main_label)
main_view.addSubview(sub_label)
# Create the details view
details_view = UIView.alloc.initWithFrame([[10, main_view.frame.size.height + 10],[300, 25]])
details_view.backgroundColor = BubbleWrap.rgb_color(236, 239, 245)
details_view.addSubview(time_label)
# Create the cell
cell = UITableViewCell.alloc.initWithStyle(UITableViewCellStyleDefault, reuseIdentifier:nil)
cell.selectionStyle = UITableViewCellSelectionStyleNone
# Set the data and return cell
cell.addSubview(main_view)
cell.addSubview(details_view)
end