0

我创建了一个tableview有按钮,这个按钮已经改变了图像屁股不同的条件。第一次加载tableview,这些按钮设置图像正确,但条件改变后,设置图像不正确。示例:如果 [Selling = 0] ,设置图像“Market.png”,否则 [Selling =2],设置图像“MarketSelect.png”。当 Selling 数组的值发生变化时,我使用了 [_tableview reloadData] 但它仍然没有改变图像。我调试,我看到 MarketButton 是 0x0000。第一次加载,MarketButton 工作正常,但第二次加载,它是 0x0000。请给我建议来解决这个问题。我尝试了 2 天,但它无法正常工作。非常感谢

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

    static NSString *CellIdentifier = @"Cell";
    CustomCell *cell = (CustomCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (nil == cell) {
        cell = [[CustomCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
    }
    cell.button.tag = indexPath.row;
                UIButton *market = [UIButton buttonWithType:UIButtonTypeCustom];
                [market setFrame:CGRectMake(200, 6, 30, 30)];
                market.tag = 4000;

                [market addTarget:self action:@selector(marketPressedAction:) forControlEvents:UIControlEventTouchDown];

                [cell.contentView addSubview:market];
                 UILabel *pricelabel = [[UILabel alloc] initWithFrame:CGRectMake(80, 0, 80, 30)];
            pricelabel.backgroundColor = [UIColor clearColor];
            pricelabel.font = [UIFont fontWithName:@"Helvetica" size:16];
            pricelabel.font = [UIFont boldSystemFontOfSize:16];
            pricelabel.textColor = [UIColor darkGrayColor];
            pricelabel.tag = 3000;
            //pricelabel.hidden = YES;
            pricelabel.textAlignment = NSTextAlignmentRight;
            [cell.contentView addSubview: pricelabel];
            [pricelabel release];
           }

            UIButton *marketButton = (UIButton*)[cell.contentView viewWithTag:4000];
            [marketButton setTag:indexPath.row];
            if([sellingArray count]>0)
            {
                NSLog(@"sellingArray %@",sellingArray);
                if([[sellingArray objectAtIndex:indexPath.row] isEqualToString:@"0"]) // nothing
                {

                    [marketButton setSelected:NO];
                    [marketButton setImage:[UIImage imageNamed:@"Marketplace.png"] forState:UIControlStateNormal];
                    marketButton.enabled = YES;

                }
                else if([[sellingArray objectAtIndex:indexPath.row] isEqualToString:@"2"])  // marketplace
                {
                    [marketButton setSelected:YES];
                    [marketButton setImage:[UIImage imageNamed:@"MarketplaceSelect.png"] forState:UIControlStateNormal];
                    marketButton.enabled = YES;

                }
            }

            }
         UILabel *pricelbl = (UILabel*)[cell.contentView viewWithTag:3000];
           pricelbl.text =[NSString stringWithFormat:@"$%@",[priceNewArray objectAtIndex:indexPath.row]];
            if ([sellingArray count]>0) {
               if([[sellingArray objectAtIndex:indexPath.row] isEqualToString:@"2"]){

                    pricelbl.hidden = NO;
                }
                else if([[sellingArray objectAtIndex:indexPath.row] isEqualToString:@"0"]){
                    pricelbl.hidden = YES;

            }


            }
            return cell;
        }

市场新闻行动:

  - (void)marketPressedAction:(id)sender
{
    buttonPressed = (UIButton *)sender;
    buttontag = buttonPressed.tag;
    NSLog(@"Market button click at row %d",buttontag);

}
4

0 回答 0