2011-06-21 150 views
0

当您滑动tableview和单元格被隐藏或删除时,是否有任何方法在UITableView中使用tableviewcell。我有这样的代码:UITableViewCell单元格不显示

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    static NSString *CellIdentifier = @"Cell"; 

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 
    if (cell == nil) { 
     cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier] autorelease]; 
    } 

    int curIndex = 0; 

    for (int i = 0; i < [dataHolder.dateArray count]; i++) 
    { 
     if ([[dataHolder.dateArray objectAtIndex:i] isEqual:[dataHolder.allDates objectAtIndex:[indexPath section]]]) 
     { 
      if ([self indexHasContains:i] == NO) 
      { 
       curIndex = i; 
       [indexHasChossen addObject:[NSString stringWithFormat:@"%i", i]]; 
       break; 
      } 
     } 
    } 

    cell.selectionStyle = UITableViewCellSelectionStyleGray; 
    cell.textLabel.text = [NSString stringWithFormat:@"%@, %@", [dataHolder.courseArray objectAtIndex:curIndex], [dataHolder.placeArray objectAtIndex:curIndex]]; 
    cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator; 
    cell.detailTextLabel.text = [NSString stringWithFormat:@"%@", [dataHolder.timeArray objectAtIndex:curIndex]]; 
    cell.selectionStyle = UITableViewCellSelectionStyleGray; 
    cell.textLabel.textColor = [UIColor blackColor]; 
    cell.detailTextLabel.textColor = [UIColor whiteColor]; 

    return cell; 
} 

我也希望从indexHasChossen阵列当细胞被隐藏/删除delets的方法。我浏览了苹果文档,还没有找到任何东西。做任何人都知道任何方式来做到这一点?

回答

0

如果您想要隐藏或删除单元格,tableView:cellForRowAtIndexPath:如何不重要。表格视图只有在知道单元格存在时才调用该方法。这取决于您在方法numberOfSectionsInTableView:tableView:numberOfRowsInSection:方法中返回的内容。大多数时候前者返回1,所以如果你想消除整个部分比你应该有某种标记,如sectionHidden这是布尔值指示部分是否隐藏。

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView { 
    if (sectionHidden) 
     return 0; 
    else 
     return 1; 
} 

何地,你要开始删除操作做这样的事,

sectionHidden = YES; 
[self.tableView reloadSections:[NSIndexSet indexSetWithIndex:0] 
       withRowAnimation:UITableViewRowAnimationFade]; 

,并翻转回做sectionHidden = NO并调用reloadSections:withRowAnimation:

同样的事情适用于行,您必须更改tableView:numberOfRowsInSection:方法以反映您已删除行或隐藏行。这次您必须使用reloadRowsAtIndexPaths:withRowAnimation:而不是reloadSections:withRowAnimation:方法。

0

虽然有“隐性” tableViewCells,用户删除的细胞被报道的tableView没有一个标准的概念:commitEditingStyle:forRowAtIndexPath:

但让我再补充一点,你似乎在的cellForRowAtIndexPath被跟踪“hasChosen”。此方法仅意味着单元即将出现在屏幕上,而不是它已被选中。当你的委托被调用tableView时发生“选择”:didSelectRowAtIndexPath:

编辑:啊,也许通过“隐藏”,你的意思是它已经离开了屏幕。不,我不相信有这样的一个呼叫,(尽管你可以欺骗一下,看看你得到的任何出队的细胞,因为那些细胞以前是在屏幕上,现在可用)。

0

从uitableview中删除单元格非常简单。我建议看看苹果开发者文档中的iPhoneCoreDataRecipes项目。

您将不得不添加一个名为commitEditingStyle的函数到您的UITableViewDelegate,并添加编辑按钮(UITableViewController中的self.editButtonItem)以允许编辑模式。

// Override to support editing the table view. 
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath { 
    if (editingStyle == UITableViewCellEditingStyleDelete) { 
    // Delete the object for the given index path 
    } 
}