2012-05-31 40 views

回答

2

你也可以繼承UITableViewCell,並創建一個子視圖的UILabel屬性,說.. trackNumber並在cellForRowAtIndex..方法初始化您的自定義單元格。

編輯:添加例子

//CustomCell.h 
@interface CustomCell : UITableViewCell 

@property(nonatomic,retain) UILabel *trackNumber; 

@end 

//CustomCell.m 
@implementation CustomCell 
@synthesize trackNumber; 

    - (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier 
    { 
     self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]; 
     if (self) { 
      // Initialization code 
      self.trackNumber = [[[UILabel alloc] initWithFrame:CGRectMake(5,5,40,40)] autorelease]; 
      [self addSubview:self.trackNumber]; 
     } 
     return self; 
    } 


    @end 

要使用它#import "CustomCell.h"和您的實現。

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


CustomCell *cell = (CustomCell*)[tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 
if (cell == nil) { 

    cell = [[[CustomCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease]; 

} 

cell.trackNumber.text = [NSString stringWithFormat:@"%i",[indexPath row]]; 

// Configure the cell. 
return cell; 
} 
1

創建自定義UITableViewCell,做任何你想用它。在檢查器中將單元格樣式更改爲「自定義」。然後吸引你想要的東西。請確保您在新標籤的視圖部分以及您在單元格中創建的其他元素上使用標籤屬性,因爲您需要這些標籤來訪問任何自定義標籤,按鈕,文本字段,無論您在單元格中做什麼。基本上你cellForRowAtIndexPath:功能,你可以拉開使用的東西你的自定義單元格狀

UILabel *numberLabel = (UILabel *)[customCell viewWithTag:1];