在ios中重新加载TableView时,UIButton为0x0000

时间:2013-08-20 07:17:20

标签: ios objective-c uitableview

我将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时,Price标签的数据变好了但是市场按钮不能改变图像。我调用[_tableview reloadData];后调试,marketButton 0x0000

2 个答案:

答案 0 :(得分: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

答案 1 :(得分:0)

当对单元格使用相同的设计时,

对单元格使用单个CellIdentifier,它用于重用tableview单元格

NSString *CellIdentifier = @"Cell"

请删除此代码[marketButton setTag:indexPath.row];您不想更改单元格的标记,不要为UIButton设置标识符的标记,更改此代码认为您的代码可以正常运行< / p>