简体   繁体   English

我想将自定义UIButton添加到UITableViewCell

[英]I want to add custom UIButton to UITableViewCell

In customCell I create some UIButtons and [self addSubview:button]; 在customCell中,我创建一些UIButtons和[self addSubview:button]; In UITableView [button addTarget:action:forControlEvents:],When I click button in cell indexPath.row = 0,the indexPath.row = 11 was clicked; 在UITableView [button addTarget:action:forControlEvents:]中,当我单击单元格indexPath.row = 0中的按钮时,单击了indexPath.row = 11; How to get clicked the button and other buttons are not affected? 如何获得单击该按钮的权限,其他按钮不受影响?

enter code here

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:
         (NSIndexPath *)indexPath
{   
    static NSString *identifier = @"cell";
    SettingTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
    if (cell == nil) {
        cell = [[[SettingTableViewCell alloc]initWithStyle:UITableViewCellStyleDefault
                  reuseIdentifier:identifier]autorelease];
    }

    if (indexPath.section == 0) {
        [cell.customBtn setTitle:@"" forState:UIControlStateNormal];
    }
    else if(indexPath.section == 1){

        if (indexPath.row == 0) {
        [cell.customBtn setTitle:@"" forState:UIControlStateNormal];
    }
    else if(indexPath.row == 1){
        [cell.customBtn setTitle:@"" forState:UIControlStateNormal];
    }
    else{
        [cell.customBtn setTitle:@"" forState:UIControlStateNormal];
    }
    else if (indexPath.section == 2){
        if (indexPath.row == 0) {
            [cell.customBtn setTitle:@"" forState:UIControlStateNormal];
        }
        else {
            [cell.customBtn setTitle:@"" forState:UIControlStateNormal];
        }
    }
    [cell.WIFIButton addTarget:self action:@selector(openOrCloseButtonClick:) 
       forControlEvents:UIControlEventTouchUpInside];
    return cell;
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 7;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    if (section == 1) {
        return 3;
    }
    else if(section == 2){
        return 2;
    }
    else if(section == 6){
        return 4;
    } 
    else{
        return 1;
    }
}


enter code here
- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        // Initialization code
        CGFloat originX = IsIOS7?10:15;
        UILabel *nameLabel = [[UILabel alloc]initWithFrame:CGRectMake(originX, 7, 150, 30)];
        nameLabel.backgroundColor = [UIColor clearColor];
        nameLabel.font = [UIFont systemFontOfSize:16];
        nameLabel.textColor = [UIColor blackColor];
        self.nameLabel = nameLabel;
        [self addSubview:nameLabel];
        [nameLabel release];
        CGFloat rightX = IsIOS7?180:160;
        UILabel *fileSizeLabel = [[UILabel alloc]initWithFrame:CGRectMake(rightX - 10, 10, 100,  
                                                                            25)];
        fileSizeLabel.backgroundColor = [UIColor clearColor];
        fileSizeLabel.font = [UIFont systemFontOfSize:14];
        fileSizeLabel.hidden = YES;
        self.fileSizeLabel = fileSizeLabel;
        [self addSubview:fileSizeLabel];
        [fileSizeLabel release];
        //This is customButton
        UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
        button.frame = CGRectMake(rightX, 10, 50, 25);
        button.backgroundColor = [UIColor grayColor];
        self.WIFIButton = button;
        [self addSubview:button];
}

Jumping the gun a little on the code, but please consider the following. 在代码上稍微跳一下枪,但是请考虑以下几点。 Add a method that can find the cell where a view appears without using tags. 添加一种无需使用标签即可找到显示视图的单元格的方法。

- (NSIndexPath *)indexPathOfSubview:(UIView *)view {
    while (view && ![view isKindOfClass:[UITableViewCell self]]) {
        view = view.superview;
    }
    UITableViewCell *cell = (UITableViewCell *)view;
    return [self.tableView indexPathForCell:cell];
}

This assumes you have an outlet called tableView pointing to a table (and it assumes that the parameter is a subview of one of the table's cells). 假定您有一个名为tableView的出口指向一个表(并且假定该参数是该表单元格之一的子视图)。 Now, on whatever method runs when a button is pressed: 现在,在按下按钮时运行的任何方法上:

- (IBAction)myButtonPressed:(id)sender {
    NSIndexPath *indexPath = [self indexPathOfSubview:sender];
    NSLog(@"button pressed in section %d, at row %d", indexPath.section, indexPath.row);
    // ta-da!
}

change static to dynamic cell 将静态更改为动态单元格

static NSString *identifier = @"cell";

to

NSString *identifier = [NSString stringWithFormat:@"cell%d",indexPath.row];

may be its working 可能是它的工作

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

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