2012-05-31 95 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];