0
我在一個應用程序中工作,需要通過customcell在單元格中添加按鈕。 任何人都可以幫助我嗎? 在此先感謝我們如何在自定義單元中添加按鈕?
我在一個應用程序中工作,需要通過customcell在單元格中添加按鈕。 任何人都可以幫助我嗎? 在此先感謝我們如何在自定義單元中添加按鈕?
在Customcell中添加一個按鈕,請使用以下代碼。 首先,您添加具有UITableViewCell的子類的新文件,然後 Customcell.h文件 @interface CustomCell:UITableViewCell { {UIButton * customButton; } @property(nonatomic,retain)UIButton * customButton;
Customcell.m
@implementation CustomCell;
@synthesize customButton;
- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
if ((self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]))
{
customButton = [UIButton buttonWithType:UIButtonTypeCustom];
[customButton setImage:[UIImage imageNamed:@"phonecall.png"] forState:UIControlStateNormal];
}
return self;
}
// set layout in subview
- (void)layoutSubviews
{
[super layoutSubviews];
CGRect contentRect = self.contentView.bounds;
CGFloat boundsX = contentRect.origin.x;
CGFloat boundY = contentRect.origin.y;
CGRect frame1;
frame1 = CGRectMake(boundsX+10, boundY+58, 19, 18);
customButton.frame = frame1;
}
#import "CustomCell.h"
In cellForRowAtIndexPath method
// Customize the number of cells in the table view.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"Cell";
CustomCell *cell= [[[CustomCell alloc] initWithFrame:CGRectZero reuseIdentifier:CellIdentifier] autorelease];
if (cell == nil)
{
cell = [[[CustomCell alloc] initWithFrame:CGRectZero reuseIdentifier:CellIdentifier] autorelease];
}
[cell.customButton addTarget:self action:@selector(callAction:) forControlEvents:UIControlEventTouchUpInside];
return cell;
}
在customcell.h \t \t BTN1 = [UIButton的buttonWithType:UIButtonTypeRoundedRect]; \t \t btn1.frame = CGRectMake(210,5,100,30); \t \t [self.contentView addSubview:btn1]; in cellforrowatindexPAth \t [cell.btn1 setTitle:@「Button 1」forState:UIControlStateNormal]; \t cell.btn1.tag = indexPath.row + 1; \t [cell.btn1 addTarget:self action:@selector(btn1_Clicked:)forControlEvents:UIControlEventTouchUpInside]; –