在customCell中,我创建了一些UIButtons和[self addSubview:button]; 在UITableView [button addTarget:action:forControlEvents:]中,当单击单元格indexPath.row = 0中的按钮时,单击indexPath.row = 11; 如何获得点击按钮和其他按钮不受影响?我想添加自定义UIButton到UITableViewCell
enter code here
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:
(NSIndexPath *)indexPath
{
static NSString *identifier = @"cell";
SettingTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
if (cell == nil) {
cell = [[[SettingTableViewCell alloc]initWithStyle:UITableViewCellStyleDefault
reuseIdentifier:identifier]autorelease];
}
if (indexPath.section == 0) {
[cell.customBtn setTitle:@"" forState:UIControlStateNormal];
}
else if(indexPath.section == 1){
if (indexPath.row == 0) {
[cell.customBtn setTitle:@"" forState:UIControlStateNormal];
}
else if(indexPath.row == 1){
[cell.customBtn setTitle:@"" forState:UIControlStateNormal];
}
else{
[cell.customBtn setTitle:@"" forState:UIControlStateNormal];
}
else if (indexPath.section == 2){
if (indexPath.row == 0) {
[cell.customBtn setTitle:@"" forState:UIControlStateNormal];
}
else {
[cell.customBtn setTitle:@"" forState:UIControlStateNormal];
}
}
[cell.WIFIButton addTarget:self action:@selector(openOrCloseButtonClick:)
forControlEvents:UIControlEventTouchUpInside];
return cell;
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return 7;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
if (section == 1) {
return 3;
}
else if(section == 2){
return 2;
}
else if(section == 6){
return 4;
}
else{
return 1;
}
}
enter code here
- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
if (self) {
// Initialization code
CGFloat originX = IsIOS7?10:15;
UILabel *nameLabel = [[UILabel alloc]initWithFrame:CGRectMake(originX, 7, 150, 30)];
nameLabel.backgroundColor = [UIColor clearColor];
nameLabel.font = [UIFont systemFontOfSize:16];
nameLabel.textColor = [UIColor blackColor];
self.nameLabel = nameLabel;
[self addSubview:nameLabel];
[nameLabel release];
CGFloat rightX = IsIOS7?180:160;
UILabel *fileSizeLabel = [[UILabel alloc]initWithFrame:CGRectMake(rightX - 10, 10, 100,
25)];
fileSizeLabel.backgroundColor = [UIColor clearColor];
fileSizeLabel.font = [UIFont systemFontOfSize:14];
fileSizeLabel.hidden = YES;
self.fileSizeLabel = fileSizeLabel;
[self addSubview:fileSizeLabel];
[fileSizeLabel release];
//This is customButton
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
button.frame = CGRectMake(rightX, 10, 50, 25);
button.backgroundColor = [UIColor grayColor];
self.WIFIButton = button;
[self addSubview:button];
}
你没有发布足够的代码以获得一个很好的答案,但是我会猜测:创建按钮tag = indexPath.row时会设置它。单元格被重用,并且与它相同的单元格(和它包含的按钮)出现在不同的行上,单击该单元格并且它具有错误的标记。发布您的按钮创建代码,以便我可以确认并提供答案。 – danh
我打破了我的水晶球,并打赌丹将在他最终的回应中使用-prepareForReuse这个词... –
代码已经上传!非常感谢! –