2013-02-11 82 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

相關問題