我爲每個視單元創建了自定義按鈕(每個單元格1個)。當滾動UITableView時,一些視單元有2個按鈕,但這並不是有意的。我沒有看到我的方式中的錯誤。UITableView按鈕在滾動時複製
請幫忙!
圖片: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:
[email protected]"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
謝謝!作品! 我在得到一個錯誤: 的UIButton *複選框= [cell.contentView viewWithTag:checkboxTag]; 「不兼容的指針類型用'UIView'類型的表達式初始化'UIButton *' – keyboardwarrior
@ user2139652我編輯了我的答案,將'-viewWithTag:'的結果投射到'UIButton'。 – Matt