滚动时UITableView按钮重复

时间:2013-11-26 21:53:24

标签: ios objective-c button tableview tableviewcell

我为每个视单元创建了自定义按钮(每个单元1个)。当滚动UITableView时,一些viewcells有2个按钮,这是不打算的。我没有看到我的错误。

请帮忙!

图片:http://s18.postimg.org/v7djg84ah/buttons_App.png

部首:

#import <UIKit/UIKit.h>

@interface ViewController : UIViewController <UITableViewDataSource>

- (NSInteger) numberOfSectionsInTableView:(UITableView *)tableView;

- (NSInteger) tableView:(UITableView *)tableView numberOfRowsInSection:    (NSInteger)section;

- (NSString *) tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section;

- (UITableViewCell *) tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath;
-(void)checkboxSelected:(id)sender;
@end

实施

#import "ViewController.h"

#define sectionCount  1
#define itemSection  0

@interface ViewController ()
{
    NSArray *items;
}
@end

@implementation ViewController

- (void)viewDidLoad
{
    [super viewDidLoad];

    items = @[@"itemdd", @"item2",@"itemdd", @"item2",@"itemdd", @"item2",@"itemdd", @"item2",@"itemdd", @"item2",@"itemdd", @"item2",@"itemdd", @"item2",@"itemdd", @"item2",@"itemdd", @"item2",@"itemdd", @"item2"];

}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

-(NSInteger) numberOfSectionsInTableView:(UITableView *)tableView
{
    return sectionCount;
}

- (NSInteger) tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    switch(section)
    {
        case itemSection:
        {
            return [items count];
        }

        default:
            return 0;

    }
}

-(NSString *) tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{
    switch (section)
    {
        case itemSection:
            return @"Items";
        default:
            return @"woot";
    }
}


-(UITableViewCell *) tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *cell =[tableView dequeueReusableCellWithIdentifier:@"itemCell"];

    switch(indexPath.section)
    {
        case itemSection:
            cell.textLabel.text = items[indexPath.row];
            break;
        default:
            cell.textLabel.text=@"Unknown";
    }
NSInteger x,y;

    x =cell.frame.origin.x +100; y = cell.frame.origin.y + 10;
    UIButton *checkbox;

    checkbox = [[UIButton alloc] initWithFrame:(CGRectMake(x,y,20,20))];

    [checkbox setBackgroundImage:[UIImage imageNamed:@"notSelectedButton.png"]forState:UIControlStateNormal];

    [checkbox setBackgroundImage:[UIImage imageNamed:@"checkedButton.png"]forState:UIControlStateSelected];
    [checkbox setBackgroundImage:[UIImage imageNamed:@"uncheckedButton.png"]forState:UIControlStateHighlighted];

    checkbox.adjustsImageWhenHighlighted = YES;
    [checkbox addTarget:self action:@selector(checkboxSelected:) forControlEvents:UIControlEventTouchDown];
    [cell.contentView addSubview:checkbox];

    return cell;
}

-(void)checkboxSelected:(id)sender
{
    bool selected = [(UIButton *)sender isSelected];
    if (selected)
    {
        [(UIButton *)sender setSelected:false];
    }
    else
    {
        [(UIButton *)sender setSelected:true];
    }


}
@end

1 个答案:

答案 0 :(得分:0)

首先,在代码中查看此语句:

UITableViewCell *cell =[tableView dequeueReusableCellWithIdentifier:@"itemCell"];

方法-dequeueReusableCellWithIdentifier将拉出已创建的表格视图单元格,或者如果没有可重复使用的单元格,则生成新的单元格。

现在回顾一下这句话:

checkbox = [[UIButton alloc] initWithFrame:(CGRectMake(x,y,20,20))];
…
[cell.contentView addSubview:checkbox];

如果您的单元格刚刚初始化,则此代码可以正常工作。但是,当您滚动表时,您的代码将开始拉出被抛入可重用队列的旧表视图单元格。那些旧的表格视图单元格已包含UIButton。因此,每次在旧单元格上调用-addSubview:时,都会添加重复按钮。

有很多方法可以解决这个问题。这是一个适合您的解决方案:

static NSInteger const checkboxTag = 123;
checkbox = (UIButton *)[cell.contentView viewWithTag:checkboxTag];
if (!checkbox) {
    checkbox = [[UIButton alloc] initWithFrame:(CGRectMake(x,y,20,20))];
    checkbox.tag = checkboxTag;

    [checkbox setBackgroundImage:[UIImage imageNamed:@"notSelectedButton.png"]forState:UIControlStateNormal];
    [checkbox setBackgroundImage:[UIImage imageNamed:@"checkedButton.png"]forState:UIControlStateSelected];
    [checkbox setBackgroundImage:[UIImage imageNamed:@"uncheckedButton.png"]forState:UIControlStateHighlighted];
    checkbox.adjustsImageWhenHighlighted = YES;
    [checkbox addTarget:self action:@selector(checkboxSelected:) forControlEvents:UIControlEventTouchDown];
    [cell.contentView addSubview:checkbox];
}

修改

还有其他值得指出的事情。这句话:

x =cell.frame.origin.x +100; y = cell.frame.origin.y + 10;

通过引用cell.frame,您将在表格视图中创建相对于单元格位置的UIButton,而不是单元格本身。设置x = 100; y = 10;以相对于单元格的边界进行偏移。