在Twitter iPhone应用程序中,在“我的个人资料”视图中,有一个包含两行的部分,其中显示了以下内容,关注者,推文和收藏夹的统计信息,每个部分似乎都具有UIButton的效果。现在我想知道它是如何创建的 - 使用每个按钮的图像?更清楚的是,它类似于以下内容。
|-----------------------|
| 23 | 11 |
| Following | tweets | <-- tableview cell 1
| | |
|-----------------------|
| 3 | 11 |
| Followers |favorites | <--- tableview cell 2
| | |
|-----------------------|
答案 0 :(得分:0)
不了解您的推特应用,但您可以创建一个自定义单元格并添加两个按钮。然后,当实例化单元格时,您可以根据需要指定选择器和自定义背景图像。
@interface CustomCell : UITableViewCell {
UIButton* leftButton;
UIButton* rightButton;
}
@property(nonatomic, retain) IBOutlet UIButton* leftButton;
@property(nonatomic, retain) IBOutlet UIButton* rightButton;
@end
@implementation CustomCell
@synthesize leftButton, rightButton;
-(id) initWithFrame:(CGRect)frame reuseIdentifier:(NSString*)reuseIdentifier {
if( self = [super initWithFrame:frame reuseIdentifier:reuseIdentifier] ) {
// init
}
return self;
}
-(void) setSelected:(BOOL)selected animated:(BOOL)animated {
[super setSelected:selected animated:animated];
// configure for selected
}
- (void)dealloc {
[rightButton release];
[leftButton release];
[super dealloc];
}
@end
// in root view controller:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = @"CustomCellIdentifier";
// Dequeue or create a cell of the appropriate type.
CustomCell *cell = (CustomCell*)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
NSArray* nib = [[NSBundle mainBundle] loadNibNamed:@"CustomCell" owner:self options:nil];
for( id oneObject in nib ) {
if( [oneObject isKindOfClass:[CustomCell class]] ) cell = (CustomCell*)oneObject;
}
}
NSInteger row = indexPath.row;
UIButton* tempButton = cell.leftButton;
tempButton.tag = row * 2;
[tempButton addTarget:self action:@selector(buttonPressed:) forControlEvents:UIControlEventTouchUpInside];
[tempButton setBackgroundImage:[thumbnails objectAtIndex:(row * 2)] forState:UIControlStateNormal];
tempButton = cell.rightButton;
tempButton.tag = (row * 2) + 1;
[tempButton addTarget:self action:@selector(buttonPressed:) forControlEvents:UIControlEventTouchUpInside];
[tempButton setBackgroundImage:[thumbnails objectAtIndex:((row * 2) + 1)] forState:UIControlStateNormal];
return cell;
}
这段代码有点陈旧,但它证明了这一点。