2012-03-22 85 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