开发者

iPhone TableViewCell - adding and removing button with condition

开发者 https://www.devze.com 2023-03-13 11:25 出处:网络
How to properly add/remove button on right side (accessory view) of a cell? How to add directory checking condition to each cell and then display one of 2 types of cell?

How to properly add/remove button on right side (accessory view) of a cell?

How to add directory checking condition to each cell and then display one of 2 types of cell?

In my case: ViewWillAppear downloads XML with directory names and loads it to TableView. Each cell is checking if the directory exists on phone. If it does then the cell is without button (off), if it doesn't the cell gets a button (on). Also ImageViews are different.

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

    static NSString *CellIdentifier = @"Cell";
    UITableViewCell *cell = [uitableview dequeueReusableCellWithIdentifier:CellIdentifier];

    SomeClass *k = (SomeClass*)[self.arr_SomeClass objectAtIndex:indexPath.row];
    NSString *checkThisDir = [documents_dir stringByAppendingPathComponent:[k.dir_name stringByDeletingPathExtension]];

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

        cell.imageView.tag = 121;
        cell.imageView.contentMode = UIViewContentModeScaleAspectFit;

        cell.selectionStyle = UITableViewCellSelectionStyleNone;
    }

    if (![[NSFileManager defaultManager] fileExistsAtPath:dir_name isDirectory:nil]) {

        cell.imageView.image = [UIImage imageNamed:@"icon_on.png"];

        UIButton *bt = [UIButton buttonWithType:UIButtonTypeRoundedRect];
        bt.contentMode = UIViewContentModeScaleAspectFit;
        bt.tag = 123;
        UIImage *imgDownload = [UIImage imageNamed:@"icon_button.png"];
        [bt setImage:imgDownload forState:UIControlStateNormal];
        [bt setImage:imgDownload forState:UIControlStateHighlighted];
        [bt setImageEdgeInsets:UIEdgeInsetsMake(4,3,2,3)];
        [bt setFrame:CGRectMake(282,5,34,34)];
        [bt addTarget:self action:@selector(listButtonClick:event:) forControlEvents:UIControlEventTouchUpInside];
        [cell addSubview:bt];

        UIView *empty = [UIView new]; // makes space for button
        cell.accessoryView = empty;
        [empty release];
    }
    else {      

        // or: cell.imageView.image = [UIImage imageNamed:@"icon_off.png"];
        UIButton *bt = (UIButton*)[cell viewWithTag:123];
        [bt removeFromSuperview];

        UIImageView *iv = (UIImageView*)[cell viewWithTag:121];
        iv.image = [UIImage imageNamed:@"icon_off.png"];
    }
    cell.detailTextLabel.text = k.details;
    cell.textLabel.text = k.name;

    return cell;    
}

listButtonClicked simply downloads a file and creates directory and then does reloadData and reloadRowsAtIndexPaths on TableView.

Problem is that when I scroll the list high or low enough then sometimes there are buttons being added to cells that shouldn't have them.

Any help would be nice, thanks in advance!

.

SOLVED:

M开发者_运维技巧oved button creation to if (cell==nil) { .. } and outside added:

UIButton *bt = (UIButton*)[cell viewWithTag:123];
if (![[NSFileManager defaultManager] fileExistsAtPath:albumDir isDirectory:nil]) {
    cell.imageView.image = [UIImage imageNamed:@"icon_on.png"];
    [bt setHidden:NO];
}
else {
    cell.imageView.image = [UIImage imageNamed:@"icon_off.png"];
    [bt setHidden:YES];
}


This happens because you are dequeuing the cells from the table (that's good!), so that iOS does not recreate them every time you need a new cell. Once the cell gets dequeued it will bring inside all the subviews you added to it before (eg. for another row). For this whole thing to work correctly, once you have dequeued a cell, you should clean all its content and then render it as you wish.

Otherwise, a better approach could be to create a cell in the if (cell == nil) block, automatically add the view for the image and the button, with no image set. Once you pass that block, you get the image using the tag ([cell viewWithTag:121]) and also the button ([cell viewWithTag:123]), and set them as hidden. From now on you can start defining the cell as required.

This way, every time you request a cell, it will have a default image and button inside, but hidden, and later you set them as visible depending on your needs.

This should solve your issue. Let me know if it helps.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号