0

我将 UIButton 添加到每个 uitableview 行,但是当我需要 reloadData 时,uibutton 为 0x0000。你能给我一些建议吗?非常感谢。我使用了这段代码:

 - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    /*NSString *CellIdentifier = [NSString stringWithFormat:@"%d,%d",indexPath.section,indexPath.row];
  //  NSString *CellIdentifier = @"cell";
    UITableViewCell *cell = [_tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil)
    {*/
    NSString *CellIdentifier = [NSString stringWithFormat:@"%d,%d",indexPath.section,indexPath.row];
    UITableViewCell *cell = [_tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier] autorelease];


        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 * market = [[UIButton alloc] init];;
        [market setFrame:CGRectMake(200, 6, 30, 30)];
         market.tag = 4000;

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

        // [market setTag:indexPath.row];

        [cell.contentView addSubview:market];
    }


    if([priceNewArray count]> 0)
    {
        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;

        }

    }


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

        }
    }

     _tableView.contentInset = UIEdgeInsetsMake(0, 0, 100, 0);

    return cell;
}

我使用上面的代码,当 reloadData 时,价格标签的数据改变得很好,但市场按钮不能改变图像。我在通话后调试[_tableview reloadData];,marketButton 是0x0000

4

2 回答 2

0

我认为你的比较是不正确的。您正在尝试将整数与字符串进行比较。

如果我认为正确然后尝试这样的事情。

int sellingPrice=0;

if([[sellingArray objectAtIndex:indexPath.row] isEqualToString: [NSString stringWithFormat:@"%i",sellingPrice]]) // nothing

 sellingPrice=2;

else if([[sellingArray objectAtIndex:indexPath.row] isEqualToString:[NSString stringWithFormat:@"%i",sellingPrice]])  // marketplace
于 2013-08-20T07:49:22.753 回答
0

当对单元格使用相同的设计时,对单元格使用单个 CellIdentifier,它用于重用 tableview 单元格

NSString *CellIdentifier = @"Cell"

请删除此代码[marketButton setTag:indexPath.row];您不想更改单元格的标签,您没有为标识符设置标签UIButton,更改这两个认为您的代码可以工作

于 2013-08-20T09:12:14.830 回答