2012-03-22 82 views
0

我想获得一些有关下面显示的代码的建议。 iPhone上的滚动有点慢,但不是在模拟器上。我想要做的是在每行显示多个小时和消息,每行可能有不同的小时数和消息数。UITableViewCell上的自定义子视图和滚动问题

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { 
    // message + hours 
    static NSString *CellIdentifier = @"Cell1"; 

    // others 
    static NSString *CellIdentifier1 = @"Cell"; 

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 
    UITableViewCell *cell1 = [tableView dequeueReusableCellWithIdentifier:CellIdentifier1]; 
    UILabel *hoursLabel; 
    UILabel *infoLabel; 
    UILabel *dayLabel; 

    switch (indexPath.section) { 
     case 0: 
      cell1 = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier1] autorelease]; 

      if ([hoursArray count] > 0 && [infoArray count] > 0) { 
       harray = [self seperateString:[hoursArray objectAtIndex:indexPath.row]]; 
       iarray = [self seperateString:[infoArray objectAtIndex:indexPath.row]]; 

       // check how many hours in an array 
       int loop = [harray count]; 
       int currentInfoHeight = 0; 
       int currentHourHeight = 0; 
       int labelHeight = 0; 

       for (int i = 0; i < loop ; i++) { 
        NSString *Text = [[NSString alloc] initWithFormat:@"%@", [harray objectAtIndex:i]]; 
        NSString *Text1 = [[NSString alloc] initWithFormat:@"%@", [iarray objectAtIndex:i]]; 
        UIFont *cellFont = [UIFont systemFontOfSize:hourfontSize]; 
        UIFont *cellFont1 = [UIFont systemFontOfSize:messageFontSize]; 
        CGSize constraintSize = CGSizeMake(180.0f, MAXFLOAT); 
        CGSize labelSize = [Text sizeWithFont:cellFont constrainedToSize:constraintSize lineBreakMode:UILineBreakModeWordWrap]; 
        CGSize labelSize1 = [Text1 sizeWithFont:cellFont1 constrainedToSize:constraintSize lineBreakMode:UILineBreakModeWordWrap]; 


        /* HourLabel */ 
        hoursLabel = 
        [[[UILabel alloc] 
         initWithFrame: 
         CGRectMake(
           70.0 + 2.0 * cell1.indentationWidth, 
           currentHourHeight + gap, 
           tableView.bounds.size.width - 70.0 - 4.0 * cell1.indentationWidth, 
           labelSize.height)] 
        autorelease]; 

        hoursLabel.text = [NSString stringWithFormat:[harray objectAtIndex:i]]; 
        hoursLabel.backgroundColor = [UIColor clearColor]; 
        hoursLabel.textColor = [UIColor blackColor]; 
        // hoursLabel.shadowColor = [UIColor blackColor]; 
        // hoursLabel.shadowOffset = CGSizeMake(0, 1); 
        hoursLabel.font = [UIFont systemFontOfSize:hourfontSize]; 
        [cell1.contentView addSubview:hoursLabel]; 

        if (![[iarray objectAtIndex:i] isEqualToString:@"-"]) { 
         /* infoLabel */ 
         infoLabel = 
         [[[UILabel alloc] 
          initWithFrame: 
          CGRectMake(
            70.0 + 2.0 * cell1.indentationWidth, 
            currentInfoHeight + gap + labelSize.height, 
            tableView.bounds.size.width - 70.0 - 4.0 * cell1.indentationWidth, 
            labelSize1.height)] 
         autorelease]; 


         infoLabel.text = [NSString stringWithFormat:[iarray objectAtIndex:i]]; 
         infoLabel.numberOfLines = 0; 
         infoLabel.backgroundColor = [UIColor clearColor]; 
         infoLabel.textColor = [UIColor colorWithRed:51.0/255 green:51.0/255.0 blue:51.0/255.0 alpha:1.0]; 
         infoLabel.font = [UIFont systemFontOfSize:messageFontSize]; 
         [cell1.contentView addSubview:infoLabel]; 
         labelHeight = (infoLabel.bounds.size.height); 
        } 
        else 
        { 
         labelHeight=0; 
        } 
        /* store current height of label */ 
        currentHourHeight = (hoursLabel.bounds.size.height) + labelHeight + gap + currentHourHeight; 
        currentInfoHeight = (hoursLabel.bounds.size.height) + labelHeight + gap + currentInfoHeight; 

       } 
      } 

      /* dayLabel */ 
      dayLabel = 
      [[[UILabel alloc] 
       initWithFrame: 
       CGRectMake(
         2.0 * cell1.indentationWidth, 
         [self tableView:tableView_ heightForRowAtIndexPath:indexPath]/2.0f - dayFontSize/2 , 
         tableView.bounds.size.width - 
         70.0 - 4.0 * cell1.indentationWidth, 
         dayFontSize)] 
      autorelease]; 
      [cell1.contentView addSubview:dayLabel]; 

      /* Configure the properties for the text that are the same on every row */ 
      dayLabel.backgroundColor = [UIColor clearColor]; 
      dayLabel.textColor = [UIColor colorWithRed:207.0/255 green:181.0/255.0 blue:59.0/255.0 alpha:1.0]; 
      dayLabel.font = [UIFont boldSystemFontOfSize:dayFontSize]; 
      /* Draw a line to divide info and message into two sections */ 
      UIView *lineView = [[[UIView alloc] initWithFrame:CGRectMake(79, 0, 1.5, cell1.contentView.bounds.size.height)] autorelease]; 
      lineView.backgroundColor = [self.tableView_ separatorColor]; 
      lineView.autoresizingMask = 0x3f; 
      [cell1.contentView addSubview:lineView]; 

      NSDateFormatter *formatter = [[NSDateFormatter alloc]init]; 
      [formatter setDateFormat:@"EEE"]; 

      dayLabel.text = [NSString stringWithFormat:[daysArray objectAtIndex:[indexPath row]]]; 

      [cell1 setSelectionStyle:UITableViewCellSelectionStyleNone]; 
      return cell1; 
     case 1: 
      if (cell == nil) 
      { 
       cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease]; 
      } 
      cell.textLabel.text = @"View information for this location"; 
      cell.textLabel.font = [UIFont systemFontOfSize:16]; 
      cell.textLabel.textAlignment = UITextAlignmentCenter; 
      return cell; 
     case 2: 
      if (cell == nil) 
      { 
       cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease]; 

      } 
      cell.textLabel.text = @"Show building on campus map"; 
      cell.textLabel.font = [UIFont systemFontOfSize:16]; 
      cell.textLabel.textAlignment = UITextAlignmentCenter; 
      return cell; 
     case 3: 
      if (cell == nil) 
      { 
       cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease]; 

      } 
      cell.textLabel.text = @"Direction to building"; 
      cell.textLabel.font = [UIFont systemFontOfSize:16]; 
      cell.textLabel.textAlignment = UITextAlignmentCenter; 

      return cell; 
     default: 
      break; 
    } 
    return cell; 
} 
+0

基本上我想在桌面视图上显示7天的小时操作,但每天可能有不同的小时数。七行高可能完全不同于彼此,我想知道如果我可以重用单元格,因为每个单元格也可能有不同数量的标签。 (例如,星期一可能有2个小时,然后是2个标签,但周二有6个小时,然后是其行中的6个标签) – paul 2012-03-22 06:25:18

回答

0

我猜,性能没有丢在这里,但在-cellForRowAtIndexPath:

你使用– prepareForReuse/– dequeueReusableCellWithIdentifier:

+0

我使用dequeueReusableCellWithIdentifier。我更新了我的代码。你可以检查出来。 – paul 2012-03-22 20:09:22

+0

但我想这并不重要,因为我从不重复使用这个单元格。 – paul 2012-03-22 20:10:00

+0

Well -cellForRowAtIndexPath:经常调用。但是,看起来你deque cell1但不会重用它。你甚至重新创建所有的子视图,而不是改变现有的内容。 – Matthias 2012-03-22 20:25:04

1
  1. 您正在为每个单元格分配一个NSDateFormatter。根据我的经验,NSDateFormatter分配和配置是一些可用的最昂贵的通话。他们需要大量的时间。
    使NSDateFormatter成为一个实例变量,因此您必须一次性分配和配置它。

  2. 你没有重复使用你的单元格。如果你不重用你的单元格,你的表现将受到影响。 重复使用的模式是这样的:

- (NSDateFormatter *)weekDayDateFormatter { 
    if (!myWeekDayDateFormatter) { 
     myWeekDayDateFormatter = [[NSDateFormatter alloc] init]; 
     [myWeekDayDateFormatter setDateFormat:@"EEE"]; 
    } 
    return myWeekDayDateFormatter; 
} 

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { 
    static NSInteger secondLabelTag = 1001; 
    static NSInteger imageViewTag = 1002; 

    static NSString *CellIdentifier1 = @"Cell1"; 
    static NSString *CellIdentifier2 = @"Cell2"; 
    UITableViewCell *cell = nil; 
    switch (indexPath.section) { 
     case 0: { 
      cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier1]; 
      if (cell == nil) { 
       cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier1]; 
       // allocate subviews and configure properties that never change 
       UILabel *secondLabel = [[UILabel alloc] initWithFrame:CGRectZero]; 
       secondLabel.tag = secondLabelTag; 
       secondLabel.textColor = [UIColor orangeColor]; 
       [cell.contentView addSubview:secondLabel]; 

       UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectZero]; 
       imageView.tag = imageViewTag; 
       imageView.contentMode = UIViewContentModeScaleAspectFill; 
       [cell.contentView addSubview:imageView]; 
      } 
      // Whatever happened before you have a valid cell here 
      UILabel *secondLabel = (UILabel *)[cell.contentView viewWithTag:secondLabelTag]; 
      UIImageView *imageView = (UIImageView *)[cell.contentView viewWithTag:imageViewTag]; 
      secondLabel.text = [self.weekDayDateFormatter stringFromDate:[dataSource objectAtIndex:indexPath.row]]; 
      imageView.image = [dataSource objectAtIndex:indexPath.row]; 
      break; 
     } 
     case 1: { 
      cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier2]; 
      if (cell == nil) { 
       cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier2]; 
       // configure properties that never change between cells 
       cell.textLabel.textColor = [UIColor greenColor]; 
       cell.selectionStyle = UITableViewCellSelectionStyleGray; 
       cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator; 
      } 
      // configure properties that are different between cells 
      cell.textLabel.text = [dataSource objectAtIndex:indexPath.row]; 
      cell.textLabel.backgroundColor = [dataSource objectAtIndex:indexPath.row]; 
      break; 
     } 

    } 

tableView:cellForRowAtIndexPath:部分被称为每次应尽可能快执行尽可能的代码。在滚动期间,每个单元格都会调用此方法。

+0

基本上我想在桌面视图上显示7天的小时操作,但每天可能有不同的小时数。七行高可能完全不同于彼此,我想知道如果我可以重用单元格,因为每个单元格也可能有不同数量的标签。 (例如星期一可能有2个小时,然后是2个标签,但周二有6个小时,然后是6个标签) – paul 2012-03-22 06:26:09

+0

http://web.missouri.edu/~bphwhc/ios.png 这是截图的链接。 – paul 2012-03-22 06:35:00