在ios中的表格单元格中单击按钮更改图像

时间:2014-07-08 07:06:39

标签: ios iphone objective-c ios7 storyboard

enter image description here

我想在按钮点击时更改按钮上的图像。该按钮放在表格单元格中。图像正在改变成功。但是当每5个单元格按钮图像也默认滚动表格后,我也没有改变它。意味着表中的新细胞,不愿意改变。这个单元格是可重用的,由故事板创建。

如何将该按钮用作切换按钮。我怎样才能做到这一点。

- (NSIndexPath *)indexPathWithSubview:(UIView *)subview {

        while (![subview isKindOfClass:[UITableViewCell self]] && subview) {
            subview = subview.superview;
        }
        return [self.table indexPathForCell:(UITableViewCell *)subview];
    }

    - (IBAction)btnCheckedCellPressed:(id)sender{
        NSIndexPath *myIP = [self indexPathWithSubview:(UIButton *)sender];
        MyWantsTableViewCell* cell = (MyWantsTableViewCell *)[self.table cellForRowAtIndexPath:myIP];
        if (cell.btnEdit.currentImage == [UIImage imageNamed:@"checkbox-active"]) {
            [cell.btnEdit setImage:[UIImage imageNamed:@"checkbox-deactive"]
                          forState:UIControlStateNormal];
        } else {
            [cell.btnEdit setImage:[UIImage imageNamed:@"checkbox-active"]
                          forState:UIControlStateNormal];
        }
    //    [self.table beginUpdates];
    //    [self.table reloadRowsAtIndexPaths:@[myIP] withRowAnimation:UITableViewRowAnimationFade];
    //    [self.table endUpdates];

    }

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{

    MyWantsTableViewCell *cell=nil;

    static NSString *AutoCompleteRowIdentifier = @"MyWantsTableViewCellEdit";

    cell = [tableView dequeueReusableCellWithIdentifier:AutoCompleteRowIdentifier forIndexPath:indexPath];

    if (cell == nil)
    {
        cell = [[MyWantsTableViewCell alloc]
                initWithStyle:UITableViewCellStyleDefault reuseIdentifier:AutoCompleteRowIdentifier];
        [[AsyncImageLoader sharedLoader] cancelLoadingImagesForTarget:cell.imgProduct];
    }
    cell.selectionStyle = UITableViewCellAccessoryNone;

    cell.btnEdit.tag = indexPath.row;
    return cell;
}

2 个答案:

答案 0 :(得分:4)

使用我在下面描述的简单方法,并使用一个对象来保持当前选择,因为如果你滚动UITableView

它会去
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{



   [cell.btnEdit setImage:[UIImage imageNamed:@"checkbox-active"]
                          forState:UIControlStateNormal];
   [cell.btnEdit setImage:[UIImage imageNamed:@"checkbox-deactive"]
                          forState:UIControlStateSelected];

   MyClass *obj=[arr objextAtIndex:indexPath.row];

   cell.btnEdit.selected=obj.selected;


}

- (IBAction)btnCheckedCellPressed:(id)sender{
        NSIndexPath *myIP = [self indexPathWithSubview:(UIButton *)sender];
        MyWantsTableViewCell* cell = (MyWantsTableViewCell *)[self.table cellForRowAtIndexPath:myIP];

        cell.btnEdit.selected=!cell.btnEdit.selected;

        MyClass *obj=[arr objectAtIndex:myIP.row];
        obj.selected=!obj.selected;

}

<强>示例

查找示例以寻求帮助here

答案 1 :(得分:2)

1.. Take a NSInteger variable like "selectedBtn" and initialize it with -1, in viewDidLoad(). 
2.. go to cellForRowAtIndexPath() set button.tag == indexpath.row; 
3.. Now in btnCheckedCellPressed() set that selectedBtn variable with tag value of that sender button. 
4.. Now again go to cellForRowAtIndexPath() and place a check 

if(cell.btnEdit.tag == selectedBtn)
    // set selected image on button 
else
   // set unselected Image 
相关问题