简体   繁体   English

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

[英]Change Image on click button in table cell in ios

在此处输入图片说明

I want to change image on the button, when button click. 单击按钮时,我想更改按钮上的图像。 That button is placed in table cell. 该按钮位于表格单元格中。 Image is changing successfull. 图像正在成功更改。 But when table scroll by default after every 5 cell button image also changed, while I didn't changed it. 但是,当默认情况下表格滚动时,每5个单元格的按钮图像也发生了变化,而我没有更改。 Means new cells in table, unwilling change. 表示表中新的单元格,不愿更改。 This cell is reusable, and created by storyboard. 该单元是可重用的,并由情节提要创建。

How to use that button as switch button. 如何使用该按钮作为切换按钮。 How can I achieve this. 我该如何实现。

- (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;
}

Use simple way as I have described below, and use an object to keep the current selection, since it will go if you scroll the UITableView 使用如下所述的简单方法,并使用一个对象保留当前选择,因为如果滚动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;

}

Sample 样品

Find sample for help here 在此处找到帮助的样本

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 

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

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