【问题标题】:Change Image on click button in table cell in ios在ios的表格单元格中单击按钮更改图像
【发布时间】:2014-08-28 19:48:09
【问题描述】:

我想在按钮单击时更改按钮上的图像。该按钮放置在表格单元格中。图像正在成功更改。但是,当每 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;
}

【问题讨论】:

    标签: ios iphone objective-c ios7 storyboard


    【解决方案1】:

    使用我在下面描述的简单方法,并使用一个对象来保持当前选择,因为如果你滚动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

    【讨论】:

    • 同样的问题,当表格滚动时,在第 5-6 个单元格被选中时。
    • @GauravDS 它会起作用,你能更新你的代码吗,你创建了一个带有属性的NSObject class 吗?你能更新你的代码吗,你实现了什么?
    • @GauravDS 在我的回答中添加了一个示例。它会帮助你。
    • 您需要在数据源中添加一个属性以确保选择安全,并且可以正确重用,因为数据源不会更改,但单元格会更改。如果您看到我的示例,我已经在我的数据源对象类中添加了一个属性 selected,我正在使用它来保存选择,并且它可以工作。
    【解决方案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 
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-04-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-02-22
      • 2016-04-21
      • 1970-01-01
      相关资源
      最近更新 更多