我正在使用Rubymotion构建iOS应用程序。 在这个程序中,我得到了许多行的tableview。每一行都有标签。主要内容标签的高度不同。因此,这些行与(按设计)高度不同。使用dequeueReusableCellWithIdentifier和自定义单元格的正确方法?
问题是dequeueReusableCellWithIdentifier。当我为所有行设置相同的值时,出现重叠行和重复项的问题。当我为每一行设置一个唯一的值时,它工作正常,但滚动性能并不理想。
处理自定义行(不同高度)的正确方法是什么?
感谢您的所有意见!
UPDATE
这是我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
可能的重复http://stackoverflow.com/questions/8030533/dynamic-uitableview-cell-height/8030716#8030716 – 2013-02-11 19:55:09
static NSString * CellIdentifier = [NSString stringWithFormat:@“yourtableViewCell”]; //我的自定义单元格 yourtableViewCell * cellObj = nil; cellObj =(yourtableViewCell *)[tableViewObj dequeueReusableCellWithIdentifier:CellIdentifier]; if(cellObj == nil){cellObj = [[[yourtableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease]; } – 2013-02-11 19:56:59
@RenoJones这是不太相关的,因为OP使用RubyMotion – 2013-02-11 20:09:19