-1

我有两个问题(实际上是一个 - 我的代码很烂):

  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

4

1 回答 1

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];
}

...并纠正其他方法...

于 2012-07-25T07:06:10.883 回答