iPhone TableViewCell-添加和删除带有条件的按钮 [英] iPhone TableViewCell - adding and removing button with condition

查看:45
本文介绍了iPhone TableViewCell-添加和删除带有条件的按钮的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何正确添加/删除单元格右侧(附件视图)上的按钮?

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?

在我的情况下:ViewWillAppear将下载具有目录名称的XML并将其加载到TableView.每个单元都在检查目录是否存在于电话中.如果是,则该单元格没有按钮(关闭),如果没有,则该单元格获得一个按钮(打开).ImageViews也不同.

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 只需下载一个文件并创建目录,然后在TableView上执行 reloadData reloadRowsAtIndexPaths .

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

问题是,当我将列表滚动得足够高或太低时,有时会在不应该包含按钮的单元格中添加一些按钮.

任何帮助都会很好,谢谢!!

Any help would be nice, thanks in advance!

.

已解决:

将按钮创建移动到 if(cell == nil){..} 并添加到外部的情况:

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

推荐答案

之所以会发生这种情况,是因为您正在从表中将单元出队(这很好!),因此iOS不会在每次需要新单元时都重新创建它们.单元一旦出队,它将带入您之前添加到其中的所有子视图(例如,另一行).为了使整个过程正常工作,在将单元格从队列中取出后,应清除其所有内容,然后根据需要进行渲染.

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.

否则,更好的方法可能是在 if(cell == nil)块中创建一个单元格,自动添加图像和按钮的视图,而未设置图像.传递完该块后,您将使用标签( [cell viewWithTag:121] )和按钮( [cell viewWithTag:123] )获取图像,并进行设置他们被视为隐藏.从现在开始,您可以根据需要开始定义单元格.

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.

这篇关于iPhone TableViewCell-添加和删除带有条件的按钮的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆