当表格向上滚动并返回到该单元格时,UITableViewCell 背景图像问题
当用户向下和向上滚动表格时,我面临 UITableViewCell 设置问题。一旦分组表滚动到另一个单元格并返回到该单元格,单元格就会丢失更改(在 -didSelectRowAtIndexPath
中设置的红色/绿色背景图像)。
我使用下面的代码在两个部分中显示一个问题和 4 个答案选项。
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
NSString *kCustomCellID = [NSString stringWithFormat:@"CustomCellID%d", ((100 * indexPath.section) + indexPath.row)];
UITableViewCell *cell = nil;//[tableView dequeueReusableCellWithIdentifier:kCustomCellID];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:kCustomCellID] autorelease];
cell.selectionStyle = UITableViewCellSelectionStyleNone;
}
if (indexPath.section == 0) {
cell.textLabel.text = questionText;
cell.textLabel.textAlignment = UITextAlignmentCenter;
cell.textLabel.font = FONT_QUESTIONTEXT;
cell.textLabel.numberOfLines = 0;
}
else {
cell.textLabel.text = [self.answerChoices objectAtIndex:indexPath.row];
cell.textLabel.font = FONT_ANSWERTEXT;
cell.textLabel.numberOfLines = 0;
ConfigurationBase *configBase = [[ConfigurationBase alloc] initWithConfigurationType:kConfigTypeQuiz];
NSString *backgroundImageName = [configBase.configInfo valueForKey:CKEY_IMG_ANSWER_BACKGROUND];
NSString *leftImage = [configBase.configInfo valueForKey:[choicesLeftImages objectAtIndex:indexPath.row]];
[configBase release];
cell.backgroundColor = [UIColor clearColor];
UIImageView *bgView = [[UIImageView alloc] initWithFrame:cell.frame];
bgView.image = [UIImage imageNamed:backgroundImageName];
cell.backgroundView = bgView;
[bgView release];
cell.imageView.image = [UIImage imageNamed:leftImage];
}
return cell;
}
并且,当用户选择其中一个选项时,我将背景颜色正确和错误的选择更改为绿色和红色。下面是更改两行背景的代码。
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
if (indexPath.section == 1) { // Answer choice section.
NSString *correctAnswerStr = [quizSession fetchCorrectAnswer];
NSInteger correctAnswerIndex = -1;
ConfigurationBase *configBase = [[ConfigurationBase alloc] initWithConfigurationType:kConfigTypeQuiz];
NSString *correctAnsImageName = [configBase.configInfo valueForKey:CKEY_IMG_ANSWER_CORRECT_BACKGROUND];
NSString *wrongAnsImageName = [configBase.configInfo valueForKey:CKEY_IMG_ANSWER_WRONG_BACKGROUND];
[configBase release];
UITableViewCell *answerCell = [tableView cellForRowAtIndexPath:indexPath];
answerCell.backgroundColor = [UIColor clearColor];
UIImageView *bgView = [[UIImageView alloc] initWithFrame:answerCell.frame];
bgView.image = [UIImage imageNamed:wrongAnsImageName];
answerCell.backgroundView = bgView;
[bgView release];
for (NSString *answerChoice in answerChoices) {
++correctAnswerIndex;
if ([answerChoice isEqualToString:correctAnswerStr]) {
// Correct Index obtained, so set the background color for it.
NSIndexPath *correctIndexPath = [NSIndexPath indexPathForRow:correctAnswerIndex
inSection:indexPath.section];
UITableViewCell *correctAnswerCell = [tableView cellForRowAtIndexPath:correctIndexPath];
correctAnswerCell.backgroundColor = [UIColor clearColor];
UIImageView *bgView = [[UIImageView alloc] initWithFrame:correctAnswerCell.frame];
bgView.image = [UIImage imageNamed:correctAnsImageName];
correctAnswerCell.backgroundView = bgView;
[bgView release];
break;
}
}
// Enable the 'next' button, allowing user to go for next question.
self.navigationItem.rightBarButtonItem.enabled = YES;
// Disable selection of answer until user go to next question.
tableView.allowsSelection = NO;
}
任何人都可以帮助我吗? TIA。
问候。
I'm facing UITableViewCell setup issue when user scrolls down and up the table. The cell looses the changes (red/green background image set in -didSelectRowAtIndexPath
) once the grouped-table scrolls to another cell and comes back to that cell.
I'm showing a question and 4 answer choices in two sections using the below code..
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
NSString *kCustomCellID = [NSString stringWithFormat:@"CustomCellID%d", ((100 * indexPath.section) + indexPath.row)];
UITableViewCell *cell = nil;//[tableView dequeueReusableCellWithIdentifier:kCustomCellID];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:kCustomCellID] autorelease];
cell.selectionStyle = UITableViewCellSelectionStyleNone;
}
if (indexPath.section == 0) {
cell.textLabel.text = questionText;
cell.textLabel.textAlignment = UITextAlignmentCenter;
cell.textLabel.font = FONT_QUESTIONTEXT;
cell.textLabel.numberOfLines = 0;
}
else {
cell.textLabel.text = [self.answerChoices objectAtIndex:indexPath.row];
cell.textLabel.font = FONT_ANSWERTEXT;
cell.textLabel.numberOfLines = 0;
ConfigurationBase *configBase = [[ConfigurationBase alloc] initWithConfigurationType:kConfigTypeQuiz];
NSString *backgroundImageName = [configBase.configInfo valueForKey:CKEY_IMG_ANSWER_BACKGROUND];
NSString *leftImage = [configBase.configInfo valueForKey:[choicesLeftImages objectAtIndex:indexPath.row]];
[configBase release];
cell.backgroundColor = [UIColor clearColor];
UIImageView *bgView = [[UIImageView alloc] initWithFrame:cell.frame];
bgView.image = [UIImage imageNamed:backgroundImageName];
cell.backgroundView = bgView;
[bgView release];
cell.imageView.image = [UIImage imageNamed:leftImage];
}
return cell;
}
And, when the user selects one of the choice, I change the background color right and wrong selection with green and red color. The code for changing the background for 2 rows is below..
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
if (indexPath.section == 1) { // Answer choice section.
NSString *correctAnswerStr = [quizSession fetchCorrectAnswer];
NSInteger correctAnswerIndex = -1;
ConfigurationBase *configBase = [[ConfigurationBase alloc] initWithConfigurationType:kConfigTypeQuiz];
NSString *correctAnsImageName = [configBase.configInfo valueForKey:CKEY_IMG_ANSWER_CORRECT_BACKGROUND];
NSString *wrongAnsImageName = [configBase.configInfo valueForKey:CKEY_IMG_ANSWER_WRONG_BACKGROUND];
[configBase release];
UITableViewCell *answerCell = [tableView cellForRowAtIndexPath:indexPath];
answerCell.backgroundColor = [UIColor clearColor];
UIImageView *bgView = [[UIImageView alloc] initWithFrame:answerCell.frame];
bgView.image = [UIImage imageNamed:wrongAnsImageName];
answerCell.backgroundView = bgView;
[bgView release];
for (NSString *answerChoice in answerChoices) {
++correctAnswerIndex;
if ([answerChoice isEqualToString:correctAnswerStr]) {
// Correct Index obtained, so set the background color for it.
NSIndexPath *correctIndexPath = [NSIndexPath indexPathForRow:correctAnswerIndex
inSection:indexPath.section];
UITableViewCell *correctAnswerCell = [tableView cellForRowAtIndexPath:correctIndexPath];
correctAnswerCell.backgroundColor = [UIColor clearColor];
UIImageView *bgView = [[UIImageView alloc] initWithFrame:correctAnswerCell.frame];
bgView.image = [UIImage imageNamed:correctAnsImageName];
correctAnswerCell.backgroundView = bgView;
[bgView release];
break;
}
}
// Enable the 'next' button, allowing user to go for next question.
self.navigationItem.rightBarButtonItem.enabled = YES;
// Disable selection of answer until user go to next question.
tableView.allowsSelection = NO;
}
Can anyone help me on this? TIA.
Regards.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您检索有关背景图像名称的信息的方法非常复杂且容易出错。请通过在适当的位置插入 NSLog 或检查调试器来确保一切按预期工作。
处理单元格的
backgroundView
属性时,您必须将此代码放入tableView:willDisplayCell:forRowAtIndexPath:
而不是cellForRowAtIndexPath
中。更简单的方法将再次节省您的时间。Your method of retrieving the information about the background image name is very complicated and error prone. Please make sure everything works as expected by inserting
NSLog
s at the appropriate places or checking in the debugger.When dealing with the cell's
backgroundView
property you will have to put this code intotableView:willDisplayCell:forRowAtIndexPath:
rather than incellForRowAtIndexPath
. A simpler approach will save you time here again.