2012-07-25 51 views
-1

我有兩個問題(其實一個 - 我的代碼很爛):我的tableView方法都表現不佳

  1. 我的表需要很長的時間來加載(約5秒)
  2. 她是可怕差

任何想法?

我的tableView方法:

// Table 
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView 
{ 
    RefreshDelegate *RefreshProtocol = [[RefreshDelegate new] autorelease]; 
    RefreshProtocol.delegate = self; 

return [[RefreshProtocol returnDataForTable] count]; 

} 

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section 
{ 
    RefreshDelegate *RefreshProtocol = [[RefreshDelegate new] autorelease]; 
    RefreshProtocol.delegate = self; 


    NSArray *curent = [self curent:section]; 
    return [curent count]; 
} 

- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section { 
    RefreshDelegate *RefreshProtocol = [[RefreshDelegate new] autorelease]; 
    RefreshProtocol.delegate = self; 

    return [[[RefreshProtocol returnDataForTable] allKeys] objectAtIndex:section]; 
} 

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

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

    NSArray *curent = [self curent:indexPath.section]; 
    cell.textLabel.text = [curent objectAtIndex:indexPath.row]; 

    return cell; 
} 

- (NSArray*)curent:(NSInteger)index { 
    RefreshDelegate *RefreshProtocol = [[RefreshDelegate new] autorelease]; 
    RefreshProtocol.delegate = self; 

    NSArray *keys = [[RefreshProtocol returnDataForTable] allKeys]; 
    NSString *curentKey = [keys objectAtIndex:index]; 
    NSArray *curent = [[RefreshProtocol returnDataForTable] objectForKey:curentKey]; 
    return curent; 
} 

我RefreshProtocol方法:

#define MaxCountPair 7 
-(NSDictionary *)returnDataForTable{ 
    NSMutableArray *day_1 = [NSMutableArray arrayWithCapacity:MaxCountPair]; 
    NSMutableArray *day_2 = [NSMutableArray arrayWithCapacity:MaxCountPair]; 
    NSMutableArray *day_3 = [NSMutableArray arrayWithCapacity:MaxCountPair]; 
    NSMutableArray *day_4 = [NSMutableArray arrayWithCapacity:MaxCountPair]; 
    NSMutableArray *day_5 = [NSMutableArray arrayWithCapacity:MaxCountPair]; 
    NSMutableArray *day_6 = [NSMutableArray arrayWithCapacity:MaxCountPair]; 


    // Analysis db and write array today 
    NSArray *array = [SQLiteAccess selectManyRowsWithSQL:@"select * from schedule"]; 
    for (int i = 0; i < [array count]; i++) { 
     NSDictionary *dictionary = [array objectAtIndex:i]; 

     if ([self checkOverlapDigit:[[[NSUserDefaults standardUserDefaults] valueForKey:@"numberWeek"] objectForKey:@"numberWeek"]:[dictionary objectForKey:@"week"]] && [self checkOverlapDigit:[self subgroupToInt]:[dictionary objectForKey:@"subgroup"]]) { 
      if ([self checkDay:[[dictionary objectForKey:@"day"] intValue]]) { 
       [day_1 addObject:[dictionary objectForKey:@"subject"]]; 
      } 
      else { 
       if ([self checkDay:[[dictionary objectForKey:@"day"] intValue] - 1]) { 
        [day_2 addObject:[dictionary objectForKey:@"subject"]]; 
       } 
       else { 
        if ([self checkDay:[[dictionary objectForKey:@"day"] intValue] - 2]) { 

         [day_3 addObject:[dictionary objectForKey:@"subject"]]; 
        } 
        else { 
         if ([self checkDay:[[dictionary objectForKey:@"day"] intValue] - 3]) { 
          [day_4 addObject:[dictionary objectForKey:@"subject"]]; 
         } 
         else { 
          if ([self checkDay:[[dictionary objectForKey:@"day"] intValue] - 4]) { 
           [day_5 addObject:[dictionary objectForKey:@"subject"]]; 
          } 
          else { 
           if ([self checkDay:[[dictionary objectForKey:@"day"] intValue] - 5]) { 
            [day_6 addObject:[dictionary objectForKey:@"subject"]]; 
           } 
          } 
         } 
        } 
       } 
      } 
     } 
    } 

    NSDictionary *days = [NSDictionary dictionaryWithObjectsAndKeys:day_1, @"1", day_2, @"2", day_3, @"3", day_4, @"4", day_5, @"5", day_6, @"6", nil]; 


    return days; 
} 

-(NSString *)removeAllButDigit:(NSString *)originalString{ 
    // Remove all but digit 
    NSMutableString *strippedString = [NSMutableString 
             stringWithCapacity:originalString.length]; 

    NSScanner *scanner = [NSScanner scannerWithString:originalString]; 
    NSCharacterSet *numbers = [NSCharacterSet 
           characterSetWithCharactersInString:@"1234"]; 

    while ([scanner isAtEnd] == NO) { 
     NSString *buffer; 
     if ([scanner scanCharactersFromSet:numbers intoString:&buffer]) { 
      [strippedString appendString:buffer]; 
     } else { 
      [scanner setScanLocation:([scanner scanLocation] + 1)]; 
     } 
    } 

    return strippedString; 
} 

-(BOOL)checkDay:(NSInteger)day{ 
    NSString *currentDay = nil; 
    switch (day) { 
     case 1: 
      currentDay = NSLocalizedString(@"_monday", nil); 
      break; 
     case 2: 
      currentDay = NSLocalizedString(@"_tuesday", nil); 
      break; 
     case 3: 
      currentDay = NSLocalizedString(@"_wednesday", nil); 
      break; 
     case 4: 
      currentDay = NSLocalizedString(@"_thursday", nil); 
      break; 
     case 5: 
      currentDay = NSLocalizedString(@"_friday", nil); 
      break; 
     case 6: 
      currentDay = NSLocalizedString(@"_saturday", nil); 
      break; 
     default: 
      break; 
    } 

    NSDateFormatter *dateFormatter = [[[NSDateFormatter alloc] init] autorelease]; 
    [dateFormatter setDateFormat:@"EEEE"]; 
    if ([currentDay isEqualToString:[dateFormatter stringFromDate:[NSDate date]]]) { 
     return YES; 
    } 
    return NO; 
} 


-(BOOL)checkOverlapDigit:(NSString *)smallerString:(NSString *)largerString{ 
    if ([largerString isEqualToString:@"0"]) { 
     return YES; 
    } 

    NSInteger intSmaller = [[self removeAllButDigit:smallerString] intValue]; 
    NSInteger intLarger = [[self removeAllButDigit:largerString] intValue]; 

    while (1) { 
     if (intLarger % 10 != 0) { 
      NSInteger sedimentWeek = intLarger % 10; 
      if (sedimentWeek == intSmaller) { 
       return YES; 
      } 
      intLarger /= 10; 
     } 
     else { 
      if (intLarger/10 != 0) { 
       intLarger /= 10; 
       if (intLarger == intSmaller) { 
        return YES; 
       } 
      } 
      else { 
       return NO; 
      } 
     } 
    } 
} 

-(NSString *)subgroupToInt{ 
    if ([[[NSUserDefaults standardUserDefaults]objectForKey:@"subgroupValue"] isEqualToString: @"subgroupValue1"]) { 
     return @"1"; 
    } 
    else 
     if ([[[NSUserDefaults standardUserDefaults]objectForKey:@"subgroupValue"] isEqualToString: @"subgroupValue2"]) { 
      return @"2"; 

     } 
     else 
      if ([[[NSUserDefaults standardUserDefaults]objectForKey:@"subgroupValue"] isEqualToString: @"subgroupValue3"]) { 
       return @"3"; 
      } 
    return @"4"; 
} 

而且SQLiteAccess類:.h.m

+0

爲什麼不在'Instruments'中分析代碼? – trojanfoe 2012-07-25 07:06:33

+0

我只是不明白什麼抑制 – Shrugged 2012-07-25 07:07:29

+0

這就是爲什麼你問'Instruments'告訴你。如果你想解決問題,那就去使用它並停止抖動。 – trojanfoe 2012-07-25 07:08:16

回答

0

對於開始...你可以(你)來創建你的對象只有一次! (在構造函數?在-viewDidLoad?)

然後在你訪問你的(唯一的)對象的tableView委託方法。

編輯: 會在您的類中的屬性:

@property (nonatomic, strong) RefreshDelegate* refreshProtocol; 

在你-viewDidLoad方法添加

-(void)viewDidLoad 
{ 
    [super viewDidLoad]; 
    self.refreshProtocol = [[RefreshDelegate new] autorelease]; 
    self.refreshProtocol.delegate = self; 
} 

現在:

- (NSArray*)curent:(NSInteger)index {  

    NSArray *keys = [[self.refreshProtocol returnDataForTable] allKeys]; 
    NSString *curentKey = [keys objectAtIndex:index]; 
    NSArray *curent = [[self.refreshProtocol returnDataForTable] objectForKey:curentKey]; 
    return curent; 
} 

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section 
{ 
    NSArray *curent = [self curent:section]; 
    return [curent count]; 
} 

...並糾正其他方法...

+0

謝謝,現在全部清楚 – Shrugged 2012-07-25 07:33:21

相關問題