ios - 用户单击完成按钮后如何更改 UIButton 的图像是 iOS 中的另一个 UIView

标签 ios objective-c uibutton

当用户点击 tableview 上的 UIButton(tableview 每行有多个按钮)时,会显示另一个 View 。 我想在用户单击另一个 UIView 的完成按钮后更改此按钮的图像。我怎样才能做到这一点?我是新手。你能为我提供一些代码吗?提前致谢。

更新代码:

表格 View 代码:

    - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSString *CellIdentifier = [NSString stringWithFormat:@"%d,%d",indexPath.section,indexPath.row];

    UITableViewCell *cell = [_tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil)
    {
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];

        UIButton *market = [UIButton buttonWithType:UIButtonTypeCustom];
        [market addTarget:self action:@selector(marketPressedAction:) forControlEvents:UIControlEventTouchDown];

        [market setTag:3000];
        [market setFrame:CGRectMake(200, 6, 30, 30)];
        [cell.contentView addSubview:market];

    }

   for (UIButton *button in [cell subviews]) { // change name of table here
    if ([button isKindOfClass:[UIButton class]]) {
        button.tag = indexPath.row;
        [button setImage:[UIImage imageNamed:@"Marketplace.png"] forState:UIControlStateNormal];
    }
}


    cell.accessoryType = UITableViewCellAccessoryDetailDisclosureButton;
    _tableView.contentInset = UIEdgeInsetsMake(0, 0, 100, 0);

    return cell;
}

打开按钮代码(用户点击显示另一个 View 的按钮)

    - (void)marketPressedAction:(id)sender
{

    UIButton *button = (UIButton *)sender;
    buttontag = button.tag;
    NSLog(@"Market button click at row %d",buttontag);
}

完成按钮的代码:

 -(void)submitMarket
{
    for (UIButton *button in [_tableView subviews]) { // change name of table here
        if ([button isKindOfClass:[UIButton class]]) {
            if (button.tag == buttontag) {
                [button setBackgroundImage:[UIImage imageNamed:@"MarketplaceSelect.png"] forState:UIControlStateNormal];
            } 
        } 
    }
}

最佳答案

试试这个:

[yourButton setBackgroundImage:someImge forState:UIControlStateNormal];

点击按钮的代码:

 savedTag = button.tag;

点击完成按钮的代码:

for (UIButton *button in [table subviews]) { // change name of table here 
  if ([button isKindOfClass:[UIButton class]]) { 
    if (button.tag == savedtag) { 
       [button setBackgroundImage:someImge forState:UIControlStateNormal];
    } 
  } 
}

在 cellForRowAtIndexPath 中代替此:[market setTag:3000]; 写入 [market setTag:indexPath.row];

试试这个:

替换:

marketButton = (UIButton *)[cell.contentView viewWithTag:3000];
[marketButton setTag:indexPath.row];

有了这个:

for (UIButton *button in [cell subviews]) { // change name of table here
     if ([button isKindOfClass:[UIButton class]]) {
         button.tag == indexPath.row; 
     }
}

还有一件事:将 cellForRowAtIndexPath 的第一行设为:

NSString *CellIdentifier = @"tableCell";

将您的 cellForRowAtIndexPath 更改为:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSString *CellIdentifier = @"cell";

    UITableViewCell *cell = [_tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil)
    {
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];

        UIButton *market = [UIButton buttonWithType:UIButtonTypeCustom];
        [market addTarget:self action:@selector(marketPressedAction:) forControlEvents:UIControlEventTouchDown];

        [market setImage:[UIImage imageNamed:@"Marketplace.png"] forState:UIControlStateNormal];

        [market setTag:indexPath.row];
        [market setFrame:CGRectMake(200, 6, 30, 30)];
        [cell.contentView addSubview:market];

    }

    else {

       for (UIButton *button in [cell subviews]) { // change name of table here
         if ([button isKindOfClass:[UIButton class]]) {
            button.tag = indexPath.row;
         }
    }
}


    cell.accessoryType = UITableViewCellAccessoryDetailDisclosureButton;
    _tableView.contentInset = UIEdgeInsetsMake(0, 0, 100, 0);

    return cell;
}

关于ios - 用户单击完成按钮后如何更改 UIButton 的图像是 iOS 中的另一个 UIView,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18247704/

相关文章:

ios - 使用 Realm,我应该使用 List 对象还是 Results 对象作为 UITableView 的数据源?

iphone - iOS 6中的"Do Not Disturb"功能如何实现?

ios - 以编程方式将 UIButton 添加到 UITableView 但按钮不显示

ios - 当我第一次快速点击按钮时,按钮单击不适用于 UITapGestureRecognizer

ios - 微软的iOS桥IslandWood是如何实现的?

ios - NSNumber double 有许多小数位被四舍五入/截断

objective-c - NSOutlineView 不更新

objective-c - 如何计算两个日期之间的差异?

objective-c - 如何在 UIImageView 上方显示一个不可见的交互按钮

iphone - 使用单个方法调用设置 UIButton 的标题?