【问题标题】:messy UITableViewCell with expand/collapse带有展开/折叠的凌乱 UITableViewCell
【发布时间】:2011-08-05 04:57:49
【问题描述】:

我正在尝试按照here 的说明实现展开/折叠表格视图单元格。我所做的修改是我希望这是一个完整的展开/折叠表视图。所以每个部分都可以折叠/展开。我查看了 Apple 的示例,这似乎是矫枉过正。

我目前面临的问题主要是格式和颜色问题。功能似乎很好。我希望节标题有白色背景,行有蓝色背景。这是我的代码:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    static NSString *CellIdentifier = @"Cell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[[UITableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier:CellIdentifier] autorelease];
    }

    if (dimension.selectedSegmentIndex == 0){
       if (!indexPath.row)
          {
              cell.textLabel.font = [UIFont fontWithName:@"Arial-BoldMT" size:18];
              cell.textLabel.text = [timeslot objectAtIndex:indexPath.section];
              if ([expandedSections containsIndex:indexPath.section])
              {
                  cell.accessoryView = [DTCustomColoredAccessory accessoryWithColor:[UIColor grayColor] type:DTCustomColoredAccessoryTypeUp];
              }
              else
              {
                  cell.accessoryView = [DTCustomColoredAccessory accessoryWithColor:[UIColor grayColor] type:DTCustomColoredAccessoryTypeDown];
              }
          }
          else
          {
              // all other rows
              cell.contentView.backgroundColor = [UIColor colorWithRed:0 green:0.188235 blue:0.313725 alpha:1];
              cell.textLabel.backgroundColor = [UIColor colorWithRed:0 green:0.188235 blue:0.313725 alpha:1];
              cell.textLabel.font = [UIFont fontWithName:@"Arial" size:14];
              cell.textLabel.textColor = [UIColor whiteColor];
              NSArray *listItems = [[timeslot objectAtIndex:indexPath.section] componentsSeparatedByString:@":"];
              if (indexPath.row == 1){
                  cell.textLabel.text = [NSString stringWithFormat:@"%@:00 - %@:15", [listItems objectAtIndex:0], [listItems objectAtIndex:0]];
              }
              if (indexPath.row == 2){
                 cell.textLabel.text = [NSString stringWithFormat:@"%@:15 - %@:30", [listItems objectAtIndex:0], [listItems objectAtIndex:0]];
              }
              if (indexPath.row == 3){
                 cell.textLabel.text = [NSString stringWithFormat:@"%@:30 - %@:45", [listItems objectAtIndex:0], [listItems objectAtIndex:0]];
              }
              if (indexPath.row == 4){
                  cell.textLabel.text = [NSString stringWithFormat:@"%@:45 - %d:00", [listItems objectAtIndex:0], ([[listItems objectAtIndex:0] intValue] + 1)];
              }

            }

        }


    if (dimension.selectedSegmentIndex == 1){
        Discount * discount = [tableData objectAtIndex:indexPath.row];
        cell.textLabel.text = discount.vendor; 
    }

    return cell;
}




- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [manager loadObjectsAtResourcePath:@"/route.json" objectClass:[Location class] delegate: self];

    RouteMapViewController * routemap = [[RouteMapViewController alloc] initWithNibName:@"RouteMapViewController" bundle:nil];
    UIBarButtonItem *backButton =    [[UIBarButtonItem alloc] initWithTitle:@"Back"
                                                                      style: UIBarButtonItemStyleBordered
                                      // style: UIBarButtonItemStylePlain
                                      // style: UIBarButtonItemStyleDone
                                                                     target:nil
                                                                     action:nil];
    self.navigationItem.backBarButtonItem = backButton;
    [backButton release];

    if (dimension.selectedSegmentIndex == 0){
            if (!indexPath.row)
            {
                // only first row toggles expand/collapse
                [preference deselectRowAtIndexPath:indexPath animated:YES];

                NSInteger section = indexPath.section;
                BOOL currentlyExpanded = [expandedSections containsIndex:section];
                NSInteger rows;

                NSMutableArray *tmpArray = [NSMutableArray array];

                if (currentlyExpanded)
                {
                    rows = [self tableView:preference numberOfRowsInSection:section];
                    [expandedSections removeIndex:section];

                }
                else
                {
                    [expandedSections addIndex:section];
                    rows = [self tableView:preference numberOfRowsInSection:section];
                }


                for (int i=1; i<rows; i++)
                {
                    NSIndexPath *tmpIndexPath = [NSIndexPath indexPathForRow:i 
                                                                   inSection:section];
                    [tmpArray addObject:tmpIndexPath];
                }

                UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];

                if (currentlyExpanded)
                {
                    [preference deleteRowsAtIndexPaths:tmpArray 
                                     withRowAnimation:UITableViewRowAnimationTop];

                    cell.accessoryView = [DTCustomColoredAccessory accessoryWithColor:[UIColor grayColor] type:DTCustomColoredAccessoryTypeDown];

                }
                else
                {
                    [preference insertRowsAtIndexPaths:tmpArray 
                                     withRowAnimation:UITableViewRowAnimationTop];
                    cell.accessoryView =  [DTCustomColoredAccessory accessoryWithColor:[UIColor grayColor] type:DTCustomColoredAccessoryTypeUp];

                }
            }

     }

这是我期望的样子(该部分的字体是错误的,因为我将它设置为 Arial Bold,但它不在这里)

这是我在展开底部时得到的:

在我滚动 UITableView 以展开稍后的条目后,这种混乱开始发生......我想这与更新视图有关..

【问题讨论】:

  • 我们看不到图片?你在使用 viewForSection 方法吗?
  • no.. 没有 viewForSection 方法。你指的是什么图片?
  • 因为我看不到图片,请您告诉我发生了什么奇怪的事情?
  • 我希望该部分具有白色背景,并且行没有披露按钮..我在代码中设置了它..但是你可以看到行有披露按钮
  • 我编辑了上面的图片和问题,希望有点清楚

标签: iphone objective-c uitableview


【解决方案1】:

要更改行的外观,您应该在cellForRowAtIndexPath 方法中更改它们。对该方法进行适当的更改。

要更改 tableView 的标题,您可以使用以下代码...

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{


        UIView *tempView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 320, 44)];
        [tempView setBackgroundColor:[UIColor whiteColor]];
        UILabel *tempLabel = [[UILabel alloc]initWithFrame:CGRectMake(10, 0, 100, 44)];
        [tempLabel setBackgroundColor:[UIColor clearColor]];
        [tempLabel setTextColor:[UIColor blackColor]];
        [tempLabel setFont:[UIFont boldSystemFontOfSize:16.0]];
        [tempLabel setText:[sectionTitles objectAtIndex:section]];
        [tempView addSubview:tempLabel];
        [tempLabel release];
        return [tempView autorelease];

}

谢谢,

【讨论】:

  • 这行不通...我希望你看看代码,看看为什么这行不通..这是一个展开/折叠的行
【解决方案2】:

这是细胞重复使用问题。每次重用表格单元格时,您都需要重置/设置每个单元格属性。

例如 cell.accessoryView 是上次使用该单元格时发生的任何事情。尝试将其设置为 nil。

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
    // Creating NEW cell
    cell = [[[UITableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier:CellIdentifier] autorelease];
}

// Now you are using either NEW or OLD cell
// You MUST set each cell property that can be different

// code to customize cell for certain purpose

return cell;

}

根据部分和行,您似乎正在设置 cell.textLabel.font、cell.textLabel.text、cell.accessoryView、cell.contentView.backgroundColor、cel.textLabel.backgroundColor 等。

细胞重复使用意味着有时您会得到一个旧细胞,就像上次使用的一样。因此,您需要设置之前设置的所有这些值。如果某些值始终相同,则在创建分支新单元格时仅定义一次。

是的,工作量很大。您可以更改所有这些长 if 子句以切换案例,开始使用通过索引访问的预定义​​数组,也许还有不同单元格的 Interface Builder XIB。如果您愿意,您还可以为不同类型的单元格使用几个不同的单元格标识符。在这种情况下,重用问题较少,但更容易出错......

【讨论】:

  • 嗯...我在哪里重置?
猜你喜欢
  • 1970-01-01
  • 2011-07-08
  • 1970-01-01
  • 2020-08-30
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-12-13
  • 2014-12-25
相关资源
最近更新 更多