所以我一直试图从前天开始解决这个问题,但没有运气。我有一个有九个单元格的UITableView,问题是当我的表格视图显示时,它只显示前6个单元格的数据,这些数据在屏幕上可见,经过一些阅读后我知道我应该重新加载该部分而不是只是[tableview reload]
,因为它只重新加载屏幕上可见的单元格,我尝试过,但它没有解决问题。这就是我的UITableView的样子:
如果我快速向上滚动,第一个单元格变空,即没有开始和结束日期,但现在最后一个单元格有开始和结束日期。
此外,如果我滚动到最后,然后单击取消并再次使此视图显示所有单元格具有开始和结束值。我不知道为什么会这样。任何帮助将不胜感激。我的cellForRowAtIndexPath
如下:
- (UITableViewCell *)tableView:(UITableView *)tableView1 cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"Cell";
UITableViewCell *Cell;
if(indexPath.section == 1)
{
NSArray *nib = [[NSBundle mainBundle] loadNibNamed:@"StageCloseCell" owner:self options:nil];
if(indexPath.row == 0)
{
Cell = [tableView1 dequeueReusableCellWithIdentifier:@"CloseCell"];
if (readOnly || [dealStagesFeature isEqualToString:@"VIEW"])
{
[closedNoDecisionBtn setHidden:YES];
[closedLostBtn setHidden:YES];
[closedWonBtn setHidden:YES];
}
if ([dealSelected.stage isEqualToString:@"Closed Won"] ||
[dealSelected.stage isEqualToString:@"Closed Lost"] ||
[dealSelected.stage isEqualToString:@"Closed-NoDecision"])
{
[closedNoDecisionBtn setHidden:YES];
[closedLostBtn setHidden:YES];
[closedWonBtn setHidden:YES];
}
if (Cell == nil)
{
Cell = (UITableViewCell *)[nib objectAtIndex:0];
closeDateTxtFld.text = [formatterForDisplay stringFromDate:dealCloseDt];
//[self enableCloseButtons];
if([dealSelected.import_source_type isEqualToString:@"SALESFORCE"])
[closedNoDecisionBtn setHidden:YES];
[closeLbl setHidden:YES];
}
}
return Cell;
}
StageCell *cell = (StageCell*)[tableView1 dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil)
{
cell = [[StageCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
if(indexPath.row == 9)
[cell.contentView addSubview:cell.mClosedSegCtrl];
AppDelegate *app = (AppDelegate *)[[UIApplication sharedApplication] delegate];
A2OTemplateStage *stage = [stages objectAtIndex:[indexPath row]];
NSLog(@"Deal Stage = %@",stage);
[[cell mStageNameLbl] setText:[NSString stringWithFormat:@"%@",stage.stage_name]];
[[cell mStartDateTextField] setTag:START_DATE_TEXTFIELD_CELL_TAG*([indexPath row]+1)];
[cell mStartDateTextField].delegate = self;
[[cell mEndDateTextField] setTag:END_DATE_TEXTFIELD_CELL_TAG*([indexPath row]+1)];
[cell mEndDateTextField].delegate = self;
[[cell mSatusLbl] setTag:COMPLETE_BTN_TAG*([indexPath row]+1)];
[[cell mCompleteBtn] setTag:COMPLETE_BTN_TAG*([indexPath row]+1)];
[[cell mCompleteBtn] addTarget:self action:@selector(satgeComplete:) forControlEvents:UIControlEventTouchUpInside];
[[cell mSkipBtn] setTag:SKIP_BTN_TAG*([indexPath row]+1)];
[[cell mSkipBtn] addTarget:self action:@selector(stageSkip:) forControlEvents:UIControlEventTouchUpInside];
cell.selectionStyle=UITableViewCellSelectionStyleNone;
//cell.accessoryType=UITableViewCellAccessoryCheckmark;
return cell;
}
**编辑**
-(void) reloadControls :(BOOL) disableAll
{
// int noOfStages = validStageCount;
if(!mStagesArr || [mStagesArr count]==0 || [mStagesArr count] < validStageCount)
return;
for(int i=0; i<validStageCount; i++)
{
StageData *stageData = mStagesArr[i];
StageCell *cell = (StageCell*)[self.tableView cellForRowAtIndexPath:[NSIndexPath indexPathForRow:i inSection:0]];
UITextField *startField = [cell mStartDateTextField];
UITextField *endField = [cell mEndDateTextField];
[startField setBackgroundColor:[UIColor whiteColor]];
[endField setBackgroundColor:[UIColor whiteColor]];
[startField setTextColor:[UIColor blackColor]];
[endField setTextColor:[UIColor blackColor]];
if(stageData.state == stageSkip)
{
cell.accessoryType = UITableViewCellAccessoryNone;
[cell.mCompleteBtn setHidden:YES];
[cell.mSkipBtn setHidden:YES];
[cell.mSatusLbl setHidden:NO];
[cell.mSatusLbl setText:@"Skipped"];
[startField setText:@""];
[startField setBackgroundColor:[UIColor whiteColor]];
// [startField setPlaceholder:@"Start Date"];
[startField setPlaceholder:[formatterForDisplay stringFromDate:stageData.startDate]];
// [endField setPlaceholder:@"End Date"];
[endField setText:@""];
[endField setBackgroundColor:[UIColor whiteColor]];
continue;
}
if(stageData.state == stageInProgress)
{
if(readOnly)
{
[cell.mSatusLbl setHidden:NO];
[cell.mSatusLbl setText:@"In Progress"];
[cell.mCompleteBtn setHidden:YES];
[cell.mSkipBtn setHidden:YES];
}
else
{
[cell.mCompleteBtn setHidden:NO];
[cell.mSkipBtn setHidden:NO];
if([stageData.startDate compare:[NSDate date]] == NSOrderedDescending)
[cell.mCompleteBtn setEnabled:NO];
[cell.mSatusLbl setHidden:YES];
}
}
if(stageData.state == stageComplete)
{
[cell.mCompleteBtn setHidden:YES];
[cell.mSkipBtn setHidden:YES];
[cell.mSatusLbl setHidden:NO];
//[cell bringSubviewToFront:cell.mSatusLbl];
[cell.mSatusLbl setText:@"Completed"];
[startField setBackgroundColor:[UIColor colorWithRed:220/255.0f green:220/255.0f blue:220/255.0f alpha:1.0]];
[endField setBackgroundColor:[UIColor colorWithRed:220/255.0f green:220/255.0f blue:220/255.0f alpha:1.0]];
[startField setTextColor:[UIColor blackColor]];
[endField setTextColor:[UIColor blackColor]];
}
if(stageData.state == stageFuture)
{
[cell.mCompleteBtn setHidden:YES];
[cell.mSkipBtn setHidden:YES];
[cell.mSatusLbl setHidden:YES];
}
[startField setPlaceholder:[formatterForDisplay stringFromDate:stageData.startDate]];
[startField setBackgroundColor:[UIColor colorWithRed:220/255.0f green:220/255.0f blue:220/255.0f alpha:1.0]];
if(stageData.state == stageComplete)
{
[endField setText:@""];
[endField setPlaceholder:[formatterForDisplay stringFromDate:stageData.endDate]];
}
else
{
[endField setText:[formatterForDisplay stringFromDate:stageData.endDate]];
[endField setPlaceholder:@""];
}
if(readOnly)
{
[cell.mCompleteBtn setHidden:YES];
[cell.mSkipBtn setHidden:YES];
[startField setEnabled:NO];
[startField setTextColor:[UIColor blackColor]];
[endField setEnabled:NO];
[endField setTextColor:[UIColor blackColor]];
[closedLostBtn setHidden:YES];
[closedWonBtn setHidden:YES];
[closedNoDecisionBtn setHidden:YES];
[closeLbl setHidden:NO];
[closeLbl setText:[self dealCloseStatus]];
[closeDateTxtFld setTextColor:[UIColor grayColor]];
[closeDateTxtFld setEnabled:NO];
}
if(disableAll)
{
[cell.mCompleteBtn setEnabled:NO];
[cell.mSkipBtn setEnabled:NO];
[cell.mSatusLbl setEnabled:NO];
[startField setEnabled:NO];
[endField setEnabled:NO];
[closeDateTxtFld setEnabled:NO];
// [closeDateTxtFld setTextColor:[UIColor grayColor]];
[closeDateTxtFld setTextColor:[UIColor blackColor]];
[closedLostBtn setEnabled:NO];
[closedWonBtn setEnabled:NO];
[closedNoDecisionBtn setEnabled:NO];
continue;
}
if ([dealStagesFeature isEqualToString:@"VIEW"])
{
[cell.mCompleteBtn setHidden:YES];
[cell.mSkipBtn setHidden:YES];
[startField setEnabled:NO];
[startField setTextColor:[UIColor blackColor]];
[endField setEnabled:NO];
[endField setTextColor:[UIColor blackColor]];
[closedLostBtn setHidden:YES];
[closedWonBtn setHidden:YES];
[closedNoDecisionBtn setHidden:YES];
[closeLbl setHidden:YES];
[closeDateTxtFld setTextColor:[UIColor grayColor]];
[closeDateTxtFld setEnabled:NO];
}
}
closeDateTxtFld.text = [formatterForDisplay stringFromDate:dealCloseDt];
//[self enableCloseButtons];
}
编辑(答案)
通过调用reloadControls
中的方法cellForRowAtIndexPath
解决了该问题。谢谢ThXou。
答案 0 :(得分:0)
我认为重点是,每次向下或向上滚动时,tableView都会重新加载并重新使用单元格。因此,您应该在cellForRowAtIndexPath:
中为第0部分启用/禁用,隐藏/取消隐藏UI组件,以便告知重用的单元格必须显示哪些视图。就像你在第1部分中所做的那样。
但是,子类化UITableViewCell并在子类中实现这种行为,我认为这是一种最佳实践,可以帮助您更好地组织代码。