2014-03-19 72 views
0

在customCell中,我创建了一些UIButtons和[self addSubview:button]; 在UITableView [button addTarget:action:forControlEvents:]中,当单击单元格indexPath.row = 0中的按钮时,单击indexPath.row = 11; 如何获得点击按钮和其他按钮不受影响?我想添加自定义UIButton到UITableViewCell

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]; 
} 
+3

你没有发布足够的代码以获得一个很好的答案,但是我会猜测:创建按钮tag = indexPath.row时会设置它。单元格被重用,并且与它相同的单元格(和它包含的按钮)出现在不同的行上,单击该单元格并且它具有错误的标记。发布您的按钮创建代码,以便我可以确认并提供答案。 – danh

+0

我打破了我的水晶球,并打赌丹将在他最终的回应中使用-prepareForReuse这个词... –

+0

代码已经上传!非常感谢! –

回答

0

在代码上跳了一下枪,但请考虑以下事项。添加一种方法,可以在不使用标签的情况下查找出现视图的单元格。

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

这里假设你有一个名为的tableView指向一个表的出口(并假设该参数是表的单元格中的一个子视图)。现在,无论当按钮被按下方法运行:

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

当我点击按钮我得到的位置,非常感谢你! –

0

变静为动细胞

static NSString *identifier = @"cell"; 

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

可能是其工作

+0

非常感谢! NSString * identifier = [NSString stringWithFormat:@“cell%d”,indexPath.row];我无法重复使用。 –

相关问题