0

ビューセルごとにカスタムボタンを作成しました(セルごとに1つ)。UITableView をスクロールすると、一部のビューセルに 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