2013-02-11 51 views
1

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

可能的重复http://stackoverflow.com/questions/8030533/dynamic-uitableview-cell-height/8030716#8030716 – 2013-02-11 19:55:09

+0

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

+0

@RenoJones这是不太相关的,因为OP使用RubyMotion – 2013-02-11 20:09:19

回答

2

您需要实现

- (Float) tableView(tableView, heightForRowAtIndexPath:indexPath) 

,并返回相应的高度,每个单元

def tableView tableView, heightForRowAtIndexPath: indexPath 
    # calculate the required height 
end 

更新

好让我看到了什么错误。

对于这样的UITableViewCell我可能会创建一个子类,然后使用它。这看起来像

def tableView(tableView, cellForRowAtIndexPath:indexPath) 
    cell = tableView.dequeueReusableCellWithIdentifier 'MyCell' 
    unless cell 
     cell = MyCell.alloc.initWithStyle(UITableViewCellStyleDefault, reuseIdentifier: 'MyCell') 
    end 

    data = @messages[indexPath.row] 

    cell.main_label.text = data.sender 
    cell.sub_label.text = data.message 
    # ... 
end 

或者,如果你不想子类,那么你需要确保你只添加视图的一次。你可以在unless块中这样做,因为每个单元格只会被调用一次,但是你需要有一些方法来查找每个子视图。找到子视图的常见方法是使用tag,但这太可怕了,所以我将子类

+0

是的,我有(见更新的问题)。 – 2013-02-11 19:35:00

+0

这就是细胞的退出/重复使用,会让事情变得糟糕。 – 2013-02-11 19:36:29

+0

究竟发生了什么问题?你是否将实现此方法的类设置为tableView的委托? 'self.tableView.delegate = self' – 2013-02-11 19:56:26

相关问题