1

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

更新代码:

表格视图代码:

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

打开按钮的代码(用户单击以显示另一个视图的按钮)

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

3 回答 3

1

尝试这个 :

[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;
}
于 2013-08-15T07:19:42.480 回答
0

您可以为按钮的选定状态设置另一个图像,单击完成按钮后,只需设置按钮的选定状态

  [yourButton setBackgroundImage:someImge forState:UIControlStateSelected];

完成按钮后:

  [yourButton setSelected:YES];

或者,如果您使用之前UIControlStateSelected还可以使用任何其他状态。甚至用户状态 -UIControlStateApplication

于 2013-08-15T07:06:07.247 回答
0

您可以使用委托或 NSNotificationCenter,在您的视图中完成按钮的位置,您需要创建一个属性

  @property (weak, nonatomic) <protocol_name> delegate;

在您的 DoneButton 操作方法中,您需要向该委托发送消息

[self.delegate method_name];

另一个视图将被设置为这个视图的委托

`[viewWithDoneButton setDelegate:self];

你需要实现委托方法`

于 2013-08-15T07:10:05.390 回答