我正在开发一个应用程序使用Xcode 4.6.1> iOS 5.0版本。UITableView怪异行为
该应用程序仅显示从服务器端获取的调查问卷,保存在数据库中并允许其用户填写。
要显示问题,我有UIScrollView
,我正在使用pagingEnabled模式。我有三种问题选择类型,分别是checkbox
,radio button
和textfield
。对于UI控件,我创建了CheckBoxButton
和RadioButton
类,它们只是从UIButton
继承而来。我通过更改背景图像来设置选择属性。因此,每个页面都包含一个问题,根据选择数量包含checkboxes | radio buttons
。如果选项数为4,则显示4个复选框。
如果问题和选择不适合页面,我创建一个UITableView
,我将它作为子视图添加到UIScrollView
。而不是使用UITableView
,我试图使用UIScrollView
,但它让事情变得更糟。所以,这不是一个真正的选择。要创建UITableViewCell
,我使用此代码。
static NSString *cellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
if(cell == nil)
{
cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];
cell.selectionStyle = UITableViewCellSelectionStyleNone ;
}
QuestionChoice *currentChoice = [[[[_questionArray objectAtIndex:currentQuestionIndex] choices] allObjects ] objectAtIndex:indexPath.row -1 ];
CheckBoxButton *checkBox = [[CheckBoxButton alloc] initWithXValue:20 yValue:0 tag:0];
[cell addSubview:checkBox];
UIFont *aeroSmall = [UIFont fontWithName:@"Aero" size:22.0f];
NSString *choiceText = currentChoice.choiceText;
CRTLabel *choiceLabel = [[CRTLabel alloc] initWithFrame:CGRectMake(80, 0, 700, 44) andText:choiceText font:aeroSmall tag:0];
[cell addSubview:choiceLabel];
我知道如果一个单元格不在屏幕上,它不存在。我的问题是与此。假设我已经选中了第四个复选框并向上滚动,所以第四个单元格不再显示。当我向下滚动时,第四个单元格将通过使用上述代码进行重新创建。我的问题是它是以旧状态创建的。这意味着当我检查之前向上滚动,它被检查重新创建。
这怎么可能?我错过了什么?我没有使用任何缓存机制。
如果未取消分配,假设第0节中的第四个单元格不显示,为什么我在cellForAtIndexPath方法后得到零,这是否正常? – limon
我不清楚评论中的问题。什么值'nil'什么时候? – vacawama
比方说,第四个单元格不在屏幕上,当我使用self.tableView cellForAtIndexPath:indexPath,对于第0和第4行,我得到零。但是,你说过屏幕外的单元没有被释放。 – limon