我正在使用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