简体   繁体   中英

iOS tableview cell handling

I have this code, where "lineaRedToday" is an UIImageView:

- (void)viewDidLoad {

[super viewDidLoad];

lineaRedToday = [UIImage imageNamed:@"line4.png"];}



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


static NSString *MyIdentifier = @"MyIdentifier";
MyIdentifier = @"tblCellView";

TableViewCell *cell = (TableViewCell*)[tableView dequeueReusableCellWithIdentifier:MyIdentifier];
if(cell == nil) {
    [[NSBundle mainBundle] loadNibNamed:@"TableViewCell" owner:self options:nil];
    cell = tblCell;
}

cell.selectionStyle = UITableViewCellSelectionStyleNone;

[[lineDay objectAtIndex:currentTodaylineRed -1] setImage:lineaRedToday];


return cell;



- (IBAction) change{

    lineaRedToday = [UIImage imageNamed:@"linea.png"];
    [lineDay setImage:lineaRedToday];
    [myTableView reloadData];
}

It is a UITableView with 15 custom UITableViewCell and I want to change the image at an UIImageView, this ImageView is "lineDay", lineDay is present in all cells, and I want change its image at all cells, but the IBAction "change" change the UIImageView only in the last cell and not at all....why?

yes it will change the image in last cell because it has the reference of only last cell, if you want to do this then when you are creating cell in cellForRowAtIndexPath check for a BOOL and set image according that BOOL. Also in IBAction change that BOOL value and call reloadData on table view to reload it. As -

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

        UITableViewCell *cellView = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];

        if (cellView == nil) 
        {
            cellView = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier] autorelease];

            UIImageView *bgImgView = [[UIImageView alloc] initWithFrame:CGRectMake(0.0f, 0.0f, 320.0f, 85.0f)];
            [bgImgView setTag:1];
            [bgImgView setBackgroundColor:[UIColor clearColor]];
            [cellView.contentView addSubview:bgImgView];
            [bgImgView release];
        }

        UIImageView *imgView = (UIImageView*)[cellView viewWithTag:1];

        if (buttonPressed) 
        {
            [imgView setImage:[UIImage imageNamed:@"listing_bg"]];
        }
        else 
        {
            [imgView setImage:[UIImage imageNamed:@"featured_listing_bg"]];
        }
        return cellView;
    }


- (IBAction) change{
    buttonPressed = !buttonPressed;
    [myTableView reloadData];
}

First, lineaRedToday is an UIImage , not an UIImageView . The first is an image, the second an object in the view hierarchy.

Then, in your code

if(cell == nil) {
    [[NSBundle mainBundle] loadNibNamed:@"TableViewCell" owner:self options:nil];
    cell = tblCell;
}

you use tblCell . I cannot see what this is. You should assign something from what you were just loading from the nib.

I don't know what lineDay is exactly, but a single view (ie UIImageView ) can only be present in one cell, not in many. If you change the image ( lineaRedToday ), you still have to set this new image in your views. There should be something like

cell.yourImageView.image = linaRedToday;

when configuring the cell.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM