2010-02-16 126 views

回答

1

創建擴展的UITableViewCell

@interface CustomTblCell : UITableViewCell 

的自定義類在對應NIB CustomTblCell.xib拖放的UILabel和UIButton的元件(並排側)。

最後,在你的方法-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath初始化並返回CustomTblCell

希望這有助於!

這裏是我上面

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{ 
    static NSString *CustomCellIdentifier = @"customCellIndentifier"; 

    CustomTblCell *cell = (CustomTblCell *)[tableView dequeueReusableCellWithIdentifier:CustomCellIdentifier]; 
    if(cell == nil){ 
     NSArray *nib = [[NSBundle mainBundle] loadNibNamed:@"CustomTblCell" owner:self options:nil]; 
     cell = [nib objectAtIndex:0]; 
    } 

    cell.label.text = @"My Product"; 
    return cell; 
} 

如果你有很多行你的臺,使得用戶通過該表使用「dequeueReusableCellWithIdentifier`滾動提到cellForRowAtIndexPath:方法的代碼段。這優化了代碼以重用現在不在用戶視圖中的同一個customTblCell對象。 deque方法將一個String作爲輸入參數(customTableCellIdentifier)。確保它與您在創建CustomTblCell時在IB中指定的標識符字符串相匹配

+0

謝謝。這樣可行。我的實際解決方案是用這個筆尖加載視圖控制器:一行而不是兩行似乎更優雅。 – Jacko 2010-02-20 03:01:43

相關問題