0

我为每个视单元创建了自定义按钮(每个单元 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
4

1 回答 1

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;为使偏移相对于单元格的边界。

于 2013-11-26T22:08:14.250 回答