0

我有一个问题:我想重新加载 tableview 但它只重新加载标签,我无法重新加载按钮的图像。这是我的代码:

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

    UITableViewCell *cell = [_tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier] autorelease];
        UILabel *FileNameLabel = [[UILabel alloc] initWithFrame:CGRectMake(10, 0, 100, 30)];
        FileNameLabel.tag = 1000;
        FileNameLabel.backgroundColor = [UIColor clearColor];
        FileNameLabel.font = [UIFont fontWithName:@"Helvetica" size:16];
        FileNameLabel.font = [UIFont boldSystemFontOfSize:16];
        FileNameLabel.textColor = [UIColor blackColor];
        NSLog(@"AllItemsArrayarray: %@", AllItemsArray);
        //  FileNameLabel.text =[temp objectAtIndex:indexPath.row];
        [cell.contentView addSubview: FileNameLabel];
        [FileNameLabel release];

        UILabel *UploadTimeLabel = [[UILabel alloc] initWithFrame:CGRectMake(10, 20, 150, 25)];
        UploadTimeLabel.tag = 2000;
        UploadTimeLabel.backgroundColor = [UIColor clearColor];
        UploadTimeLabel.font = [UIFont fontWithName:@"Helvetica" size:12];
        UploadTimeLabel.textColor = [UIColor grayColor];
        // UploadTimeLabel.text = [UploadTimeArray objectAtIndex:indexPath.row];
        [cell.contentView addSubview: UploadTimeLabel];
        [UploadTimeLabel release];
        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];

        market = [UIButton buttonWithType:UIButtonTypeCustom];
        market.tag = 4000;


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

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

    marketButton = (UIButton*)[cell.contentView viewWithTag:4000];
    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;
            // auctionButton.enabled = NO;
        }
    }
    [marketButton addTarget:self action:@selector(marketPressedAction:) forControlEvents:UIControlEventTouchDown];

    [marketButton setTag:indexPath.row];

    //}


       if( [temp count] > 0)
    {
        UILabel *fileNameLbl = (UILabel*)[cell.contentView viewWithTag:1000];
        fileNameLbl.text =[temp objectAtIndex:indexPath.row];

        UILabel *uploadlbl = (UILabel*)[cell.contentView viewWithTag:2000];
        uploadlbl.text =[UploadTimeAllArr objectAtIndex:indexPath.row];
    }
    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;

        }

        }


    }
UIImage *image = [UIImage imageNamed:@"More.png"];

    UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
    CGRect frame = CGRectMake(0.0, 0.0, image.size.width, image.size.height);
    button.frame = frame;   // match the button's size with the image size

    //[button setBackgroundImage:image forState:UIControlStateNormal];
    [button setImage:image forState:UIControlStateNormal];
    // set the button's target to this table view controller so we can interpret touch events and map that to a NSIndexSet
    [button addTarget:self action:@selector(checkButtonTapped:event:) forControlEvents:UIControlEventTouchUpInside];
    button.backgroundColor = [UIColor clearColor];
    cell.accessoryView = button;

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

    return cell;
    }

我使用上面的代码,当调用时[table reloaddata],除了按钮(按钮市场)之外的所有标签都重新加载不显示正确的图像。我不知道为什么会这样。你能给点建议吗?提前致谢

4

1 回答 1

0

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath始终遵循相同的模式。如果找到一个单元格,它将使一个单元格出列,然后由您来配置它。如果找不到单元格(nil == 单元格),则必须创建一个新单元格,然后执行配置步骤。

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *cell = [_tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil)
    {
        market = [UIButton buttonWithType:UIButtonTypeCustom];
    }
    // configure the market button
    return cell;
}

一个普遍的建议是使用故事板/界面构建器来配置您需要的单元格或子类UITableViewCell以拥有自己的单元格。这将大大简化您的代码并提高可读性。

于 2013-08-19T11:00:11.903 回答