【发布时间】:2015-08-06 18:03:27
【问题描述】:
在我的 UITableView 中,有标题单元格和标准单元格。每个标准单元格都添加了一个复选框(UIButton),使用以下两种方法。
调用下面的方法来创建每个UITableViewCell:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *tableIdentifier = @"activityIdentifier";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:tableIdentifier];
// HEADER ROWS
if (indexPath.row == 0 || indexPath.row == 5 || indexPath.row == 10 || indexPath.row == 15 || indexPath.row == 20 || indexPath.row == 25 || indexPath.row == 30) {
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:tableIdentifier];
NSLog(@"Yeaa!");
}
cell.textLabel.text = [activityArray objectAtIndex:indexPath.row];
cell.textLabel.textColor = [UIColor blackColor];
cell.textLabel.font = [UIFont fontWithName:@"Aliquam" size:13.0f];
cell.selectionStyle = UITableViewCellSelectionStyleNone;
cell.backgroundColor = [UIColor whiteColor];
static int a = 20.0f;
tableView.rowHeight = a;
} else {
// STANDARD ROWS
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:tableIdentifier];
NSLog(@"Standard cell is nil");
}
cell.textLabel.text = [activityArray objectAtIndex:indexPath.row];
cell.backgroundColor = [UIColor clearColor];
cell.textLabel.textColor = [UIColor whiteColor];
cell.textLabel.font = [UIFont fontWithName:@"Aliquam" size:20.0f];
cell.selectionStyle = UITableViewCellSelectionStyleNone;
static int b = 44.0f;
tableView.rowHeight = b;
}
// CREATE CHECK BOXES IN STANDARD ROWS
if (indexPath.row == 1) {
[self createRoleCheckbox:actMondayMorning];
[cell addSubview:actMondayMorning];
} else if (indexPath.row == 2) {
[self createRoleCheckbox:actMondayAfternoon];
[cell addSubview:actMondayAfternoon];
} else if (indexPath.row == 3) {
[self createRoleCheckbox:actMondayEvening];
[cell addSubview:actMondayEvening];
} else if (indexPath.row == 4) {
[self createRoleCheckbox:actMondayNight];
[cell addSubview:actMondayNight];
} else if (indexPath.row == 6) {
[self createRoleCheckbox:actTuesdayMorning];
[cell addSubview:actTuesdayMorning];
} else if (indexPath.row == 7) {
[self createRoleCheckbox:actTuesdayAfternoon];
[cell addSubview:actTuesdayAfternoon];
} else if (indexPath.row == 8) {
[self createRoleCheckbox:actTuesdayEvening];
[cell addSubview:actTuesdayEvening];
} else if (indexPath.row == 9) {
[self createRoleCheckbox:actTuesdayNight];
[cell addSubview:actTuesdayNight];
} else if (indexPath.row == 11) {
[self createRoleCheckbox:actWednesdayMorning];
[cell addSubview:actWednesdayMorning];
} else if (indexPath.row == 12) {
[self createRoleCheckbox:actWednesdayAfternoon];
[cell addSubview:actWednesdayAfternoon];
} else if (indexPath.row == 13) {
[self createRoleCheckbox:actWednesdayEvening];
[cell addSubview:actWednesdayEvening];
} else if (indexPath.row == 14) {
[self createRoleCheckbox:actWednesdayNight];
[cell addSubview:actWednesdayNight];
} else if (indexPath.row == 16) {
[self createRoleCheckbox:actThursdayMorning];
[cell addSubview:actThursdayMorning];
} else if (indexPath.row == 17) {
[self createRoleCheckbox:actThursdayAfternoon];
[cell addSubview:actThursdayAfternoon];
} else if (indexPath.row == 18) {
[self createRoleCheckbox:actThursdayEvening];
[cell addSubview:actThursdayEvening];
} else if (indexPath.row == 19) {
[self createRoleCheckbox:actThursdayNight];
[cell addSubview:actThursdayNight];
} else if (indexPath.row == 21) {
[self createRoleCheckbox:actFridayMorning];
[cell addSubview:actFridayMorning];
} else if (indexPath.row == 22) {
[self createRoleCheckbox:actFridayAfternoon];
[cell addSubview:actFridayAfternoon];
} else if (indexPath.row == 23) {
[self createRoleCheckbox:actFridayEvening];
[cell addSubview:actFridayEvening];
} else if (indexPath.row == 24) {
[self createRoleCheckbox:actFridayNight];
[cell addSubview:actFridayNight];
} else if (indexPath.row == 26) {
[self createRoleCheckbox:actSaturdayMorning];
[cell addSubview:actSaturdayMorning];
} else if (indexPath.row == 27) {
[self createRoleCheckbox:actSaturdayAfternoon];
[cell addSubview:actSaturdayAfternoon];
} else if (indexPath.row == 28) {
[self createRoleCheckbox:actSaturdayEvening];
[cell addSubview:actSaturdayEvening];
} else if (indexPath.row == 29) {
[self createRoleCheckbox:actSaturdayNight];
[cell addSubview:actSaturdayNight];
} else if (indexPath.row == 31) {
[self createRoleCheckbox:actSundayMorning];
[cell addSubview:actSundayMorning];
} else if (indexPath.row == 32) {
[self createRoleCheckbox:actSundayAfternoon];
[cell addSubview:actSundayAfternoon];
} else if (indexPath.row == 33) {
[self createRoleCheckbox:actSundayEvening];
[cell addSubview:actSundayEvening];
} else if (indexPath.row == 34) {
[self createRoleCheckbox:actSundayNight];
[cell addSubview:actSundayNight];
}
return cell;
}
调用下面的方法来创建复选框(UIButton):
- (void) createRoleCheckbox:(UIButton *)passedButton {
passedButton.frame = CGRectMake(screenWidth-checkboxDiameter-(checkboxInset/2), (checkboxInset/2), checkboxDiameter, checkboxDiameter);
[passedButton setImage:checkboxImageUnselected forState:UIControlStateNormal];
[passedButton setImage:checkboxImageSelected forState:UIControlStateSelected];
if (passedButton == actMondayMorning)
[passedButton addTarget:self action:@selector(actMondayMorningPressed) forControlEvents:UIControlEventTouchUpInside];
else if (passedButton == actMondayAfternoon)
[passedButton addTarget:self action:@selector(actMondayAfternoonPressed) forControlEvents:UIControlEventTouchUpInside];
else if (passedButton == actMondayEvening)
[passedButton addTarget:self action:@selector(actMondayEveningPressed) forControlEvents:UIControlEventTouchUpInside];
else if (passedButton == actMondayNight)
[passedButton addTarget:self action:@selector(actMondayNightPressed) forControlEvents:UIControlEventTouchUpInside];
else if (passedButton == actTuesdayMorning)
[passedButton addTarget:self action:@selector(actTuesdayMorningPressed) forControlEvents:UIControlEventTouchUpInside];
else if (passedButton == actTuesdayAfternoon)
[passedButton addTarget:self action:@selector(actTuesdayAfternoonPressed) forControlEvents:UIControlEventTouchUpInside];
else if (passedButton == actTuesdayEvening)
[passedButton addTarget:self action:@selector(actTuesdayEveningPressed) forControlEvents:UIControlEventTouchUpInside];
else if (passedButton == actTuesdayNight)
[passedButton addTarget:self action:@selector(actTuesdayNightPressed) forControlEvents:UIControlEventTouchUpInside];
else if (passedButton == actWednesdayMorning)
[passedButton addTarget:self action:@selector(actWednesdayMorningPressed) forControlEvents:UIControlEventTouchUpInside];
else if (passedButton == actWednesdayAfternoon)
[passedButton addTarget:self action:@selector(actWednesdayAfternoonPressed) forControlEvents:UIControlEventTouchUpInside];
else if (passedButton == actWednesdayEvening)
[passedButton addTarget:self action:@selector(actWednesdayEveningPressed) forControlEvents:UIControlEventTouchUpInside];
else if (passedButton == actWednesdayNight)
[passedButton addTarget:self action:@selector(actWednesdayNightPressed) forControlEvents:UIControlEventTouchUpInside];
else if (passedButton == actThursdayMorning)
[passedButton addTarget:self action:@selector(actThursdayMorningPressed) forControlEvents:UIControlEventTouchUpInside];
else if (passedButton == actThursdayAfternoon)
[passedButton addTarget:self action:@selector(actThursdayAfternoonPressed) forControlEvents:UIControlEventTouchUpInside];
else if (passedButton == actThursdayEvening)
[passedButton addTarget:self action:@selector(actThursdayEveningPressed) forControlEvents:UIControlEventTouchUpInside];
else if (passedButton == actThursdayNight)
[passedButton addTarget:self action:@selector(actThursdayNightPressed) forControlEvents:UIControlEventTouchUpInside];
else if (passedButton == actFridayMorning)
[passedButton addTarget:self action:@selector(actFridayMorningPressed) forControlEvents:UIControlEventTouchUpInside];
else if (passedButton == actFridayAfternoon)
[passedButton addTarget:self action:@selector(actFridayAfternoonPressed) forControlEvents:UIControlEventTouchUpInside];
else if (passedButton == actFridayEvening)
[passedButton addTarget:self action:@selector(actFridayEveningPressed) forControlEvents:UIControlEventTouchUpInside];
else if (passedButton == actFridayNight)
[passedButton addTarget:self action:@selector(actFridayNightPressed) forControlEvents:UIControlEventTouchUpInside];
else if (passedButton == actSaturdayMorning)
[passedButton addTarget:self action:@selector(actSaturdayMorningPressed) forControlEvents:UIControlEventTouchUpInside];
else if (passedButton == actSaturdayAfternoon)
[passedButton addTarget:self action:@selector(actSaturdayAfternoonPressed) forControlEvents:UIControlEventTouchUpInside];
else if (passedButton == actSaturdayEvening)
[passedButton addTarget:self action:@selector(actSaturdayEveningPressed) forControlEvents:UIControlEventTouchUpInside];
else if (passedButton == actSaturdayNight)
[passedButton addTarget:self action:@selector(actSaturdayNightPressed) forControlEvents:UIControlEventTouchUpInside];
else if (passedButton == actSundayMorning)
[passedButton addTarget:self action:@selector(actSundayMorningPressed) forControlEvents:UIControlEventTouchUpInside];
else if (passedButton == actSundayAfternoon)
[passedButton addTarget:self action:@selector(actSundayAfternoonPressed) forControlEvents:UIControlEventTouchUpInside];
else if (passedButton == actSundayEvening)
[passedButton addTarget:self action:@selector(actSundayEveningPressed) forControlEvents:UIControlEventTouchUpInside];
else if (passedButton == actSundayNight)
[passedButton addTarget:self action:@selector(actSundayNightPressed) forControlEvents:UIControlEventTouchUpInside];
}
此代码正确地在每一行中创建复选框。
但是,当我滚动浏览我的应用程序时,UIButtons 重复,随机出现在标题单元格中。
此外,如果我单击一个按钮,使其处于选中状态,当我滚动时,其他按钮也会随机处于选中状态,即使我没有按下它们。
简而言之,UIButtons 正在被重复使用。我怎么能不发生这种情况?
感谢所有帮助。
【问题讨论】:
标签: ios objective-c uitableview uibutton