2010-01-16 29 views
1

我的應用程序中的大部分視圖都是UIViewController中的UITableVlews。我的應用程序在嘗試滾動瀏覽表格時會覺得它滯後。我想知道(1)如果最好在表視圖中創建單元對象,或者在運行時創建它們並將它們添加到單元格子視圖中?應用程序視圖感覺就像是「拖拽」

例子

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { 
      case 3: 
      { 
       NSNumber *tetherState = [[mobIntDict objectForKey:@"State"] intValue]; 
       NSNumber *currValState = [[NSNumber numberWithInt:1023] intValue]; 
       tetherSw = [[UISwitch alloc] initWithFrame:CGRectMake(197, 8, 94, 27)]; 
       tetherSw.tag = kDefaultSwTag; 
       if(tetherState == currValState){ 
        tetherSw.on = YES; 
       }else{ 
        tetherSw.on = NO; 
       } 
       [tetherSw addTarget:self action:@selector(tetherSwAction:) forControlEvents:UIControlEventValueChanged]; 
       [cell.contentView addSubview:tetherSw]; 
       cell.textLabel.text = @"Tether"; 
       [tetherSw release]; 
      } 
       break; 
} 

- -

-(void)viewDidLoad{ 

    tetherSw = [[[UISwitch alloc] initWithFrame:CGRectMake(197, 8, 94, 27)] autorelease]; 
    tetherSw.tag = kDefaultSwTag; 
    [tetherSw addTarget:self action:@selector(tetherSwAction:) forControlEvents:UIControlEventValueChanged]; 
} 

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { 
      case 3: 
      { 
       [cell addSubView:tetherSw]; 
      } 
} 

回答

0

其實,我的表格設置得很好。一個堅實的恢復做的伎倆,我的應用程序運行沒有上述「滯後」

1

沒關係。你的細胞很複雜,因此你的視線滯後。

如果您需要性能,請避免UISwitch。改爲切換單元格的複選標記。事實上,只要避免任何奇特的表格視圖單元格子類或自定義背景,就可以減小視圖層次結構的大小。

+0

這是我的應用程序的主要功能類型取決於UISwitches。至於背景,我沒有任何設置,只是普通的電視廣播。 – WrightsCS 2010-01-17 07:24:13

1

您正確排隊和重用細胞嗎?

這將優化的東西很多,如果你重複使用的單元格,說@「SwitchCell」,這將加快滾動了很多。目前,將切換器添加到單元格的內容視圖(放置視圖等)和執行其他任務只需要在單元格生命週期中發生一次,而不是每次在滾動時出現新單元格時都需要花費大量時間。

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { 

    // Create cell 
    static NSString *CellIdentifier = @"SwitchCell"; 
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 
    if (cell == nil) { 
     cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease]; 
     UISwitch *tetherSw = [[UISwitch alloc] initWithFrame:CGRectMake(197, 8, 94, 27)]; 
     tetherSw.tag = kDefaultSwTag; 
     [tetherSw addTarget:self action:@selector(tetherSwAction:) forControlEvents:UIControlEventValueChanged]; 
     [cell.contentView addSubview:tetherSw]; 
     [tetherSw release]; 
    } 

    // Setup for each cell 
    cell.textLabel.text = @"Tether"; 
    NSNumber *tetherState = [[mobIntDict objectForKey:@"State"] intValue]; 
    NSNumber *currValState = [[NSNumber numberWithInt:1023] intValue]; 
    UISwitch *tetherSw = (UISwitch *)[cell.contentView viewWithTag: kDefaultSwTag]; 
    tetherSw.on = (tetherState == currValState); 

    // Return 
    return cell; 

} 

有關出列的更多信息,請參閱dequeueReusableCellWithIdentifier的文檔。

此外,請確保您的任何單元格的子視圖中都沒有透明度。這導致很多滯後。確保任何標籤或其他添加的東西都有opaque = YES和背景顏色集。