我有一個表格視圖。我可以添加和刪除單元格。以免我說我加5。當我離開表視圖(切換視圖),並返回到表視圖,單元格消失。這是爲什麼發生?另外,我怎麼能解決這個問題?謝謝!UITableView單元格消失,當表格被留下然後返回到
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath: (NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"Cell";
cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil)
{
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
stepper = [[UIStepper alloc]init];
[stepper setFrame:CGRectMake(216, 22, 100, 30)];
NSLog(@"the stepper is %@", stepper);
[cell addSubview:stepper];
cellLabel = [[UILabel alloc]init];
[cellLabel setFrame:CGRectMake(65, 22, 27, 27)];
[cellLabel setText:@"1"];
[cell.contentView addSubview:cellLabel];
}
[cell setSelectionStyle:UITableViewCellSelectionStyleNone];
cell.imageView.image = [imageArray objectAtIndex:indexPath.row];
// cell.textLabel.text = [cells objectAtIndex:indexPath.row];
if ([cell.imageView.image isEqual:[UIImage imageNamed:@"paddle5.png"]]) {
[cellLabel setFrame:CGRectMake (175, 22, 30, 30)];
}
if ([cell.imageView.image isEqual:[UIImage imageNamed:@"paddle4.png"]]) {
[cellLabel setFrame:CGRectMake (150, 22, 30, 30)];
}
if ([cell.imageView.image isEqual:[UIImage imageNamed:@"paddle3.png"]]) {
[cellLabel setFrame:CGRectMake (120, 22, 30, 30)];
}
if ([cell.imageView.image isEqual:[UIImage imageNamed:@"paddle2.png"]]) {
[cellLabel setFrame:CGRectMake (90, 22, 30, 30)];
}
return cell;
}
NumberOfRows
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return [cells count];
}
CommitEditingStyle:
- (void) tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle
forRowAtIndexPath:(NSIndexPath *)indexPath
{
if (editingStyle == UITableViewCellEditingStyleDelete)
{
[[self cells] removeObjectAtIndex:[indexPath row]];
[[self imageArray]removeObjectAtIndex:[indexPath row]];
NSArray *indexPaths = [NSArray arrayWithObject:indexPath];
[[self myTableView] deleteRowsAtIndexPaths:indexPaths withRowAnimation:UITableViewRowAnimationFade];
}
}
能否請您提供您所使用的代碼?這個問題的文本沒有給出足夠的信息來有意義地回答你的問題。 – dasblinkenlight
虐待添加cellForRow!那是你想要的代碼嗎? – iProRage
添加單元格時,是否將相應的新數據添加到數據源中? – Nevin