繁体   English   中英

如何在自定义单元格中添加按钮?

[英]How can we add button in custom cell?

我在一个应用程序中工作,需要通过customcell在单元格中添加按钮。 有人可以帮我吗? 提前致谢

您在Customcell中添加一个按钮,因此请使用以下代码。 首先,添加带有UITableViewCell子类的新文件,然后添加Customcell.h文件@interface CustomCell:UITableViewCell {UIButton * customButton; } @property(非原子的,保留)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;
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM