在cellForRowAtIndexPath中加载太多数据导致慢滚动?

时间:2013-04-03 14:03:55

标签: xml xcode parsing scroll tableview

我有以下问题:我的tableview滚动比平常慢一点。这是我的代码:

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

    // creating plist with channel logos
    NSDictionary *picturesDictionary = [NSDictionary dictionaryWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"Photos" ofType:@"plist"]];
    NSArray *imageArray = [picturesDictionary objectForKey:@"PhotosArray"];

    // creating plist with channel names
    NSDictionary *namesDictionary = [NSDictionary dictionaryWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"Names" ofType:@"plist"]];
    NSArray *namesArray = [namesDictionary objectForKey:@"NamesArray"];

    if (cell == nil)
    {
        NSArray *objects = [[NSBundle mainBundle] loadNibNamed:@"televisionCell" owner:self options:nil];
        for (id currentObject in objects)
        {
            if ([currentObject isKindOfClass:[UITableViewCell class]])
            {
                if (indexPath.row % 2)
                {
                    cell = (televisionCell *)currentObject;
                    cell.imageView.image = [UIImage imageNamed:[imageArray objectAtIndex:indexPath.row]];
                    cell.tvLabel.text = [namesArray objectAtIndex:indexPath.row];
                    cell.backgroundView.image = [UIImage imageNamed:@"lightbackgr.png"];
                    break;
                }
                else
                {
                    cell = (televisionCell *)currentObject;
                    cell.imageView.image = [UIImage imageNamed:[imageArray objectAtIndex:indexPath.row]];
                    cell.tvLabel.text = [namesArray objectAtIndex:indexPath.row];
                    cell.backgroundView.image = [UIImage imageNamed:@"background-cell.png"];
                    break;
                }
            }
        }
    }
    // getting the channel list array
    BNRAppDelegate *appDelegate = (BNRAppDelegate *)[[UIApplication sharedApplication] delegate];
    NSArray *channelArray = [appDelegate channelsArray];
    NSString *date = [appDelegate str];

    // getting the current time
    NSDateFormatter *hourFormatter = [[NSDateFormatter alloc] init];
    [hourFormatter setDateFormat:@"HH.mm"];
    NSDate *hour = [NSDate date];
    NSString *currHour = [hourFormatter stringFromDate:hour];
    float value = [currHour floatValue];

    // parse different file for every channel
    NSString *dayToString = [NSString stringWithFormat:@"http://pik.bg/TV/%@/%@.xml", [channelArray objectAtIndex:indexPath.row], date];
    NSURL *url = [NSURL URLWithString:dayToString];
    NSData *webData = [NSData dataWithContentsOfURL:url];

    // getting every <chas> element from the xml file
    NSString *xPathQueryBegin = @"//elem/chas";
    TFHpple *parserBegin = [TFHpple hppleWithXMLData:webData];
    NSArray *arrayBegin = [parserBegin searchWithXPathQuery:xPathQueryBegin];
    NSMutableArray *newArrayBegin = [[NSMutableArray alloc] initWithCapacity:0];
    for (TFHppleElement *element in arrayBegin)
    {
        Listing *begin = [[Listing alloc] init];
        begin.beginHour = [[element firstChild] content];
        if (begin.beginHour != NULL)
        {
            [newArrayBegin addObject:begin.beginHour];
        }
    }

  /*          // converting every object in the array to float number
            NSMutableArray *floatArrayBegin=[NSMutableArray new];
            for(NSString *string in newArrayBegin)
            {
                NSNumber *ourNumber = [[NSNumber alloc] initWithFloat: [string floatValue]];
                [floatArrayBegin addObject:ourNumber];
            }
   */

    // getting every <endd> element from the xml file
    NSString *xPathQueryEnd = @"//elem/endd";
    TFHpple *parserEnd = [TFHpple hppleWithXMLData:webData];
    NSArray *arrayEnd = [parserEnd searchWithXPathQuery:xPathQueryEnd];
    NSMutableArray *newArrayEnd = [[NSMutableArray alloc] initWithCapacity:0];
    for (TFHppleElement *element in arrayEnd)
    {
        Listing *end = [[Listing alloc] init];
        end.endHour = [[element firstChild] content];
        if (end.endHour != NULL)
        {
            [newArrayEnd addObject:end.endHour];
        }
    }

   /*         // converting every object in the array to float number
            NSMutableArray *floatArrayEnd = [NSMutableArray new];
            for(NSString *string in newArrayEnd)
            {
                NSNumber * ourNumber = [[NSNumber alloc] initWithFloat: [string floatValue]];
                [floatArrayEnd addObject:ourNumber];
            }
    */

    // gettin every <title> element from the xml file
    NSString *xPathQueryTitle = @"//elem/title";
    TFHpple *parserTitle = [TFHpple hppleWithXMLData:webData];
    NSArray *arrayTitle = [parserTitle searchWithXPathQuery:xPathQueryTitle];
    NSMutableArray *newArrayTitle = [[NSMutableArray alloc] initWithCapacity:0];
    for (TFHppleElement *element in arrayTitle)
    {
        Listing *title = [[Listing alloc] init];
        title.showTitle = [[element firstChild] content];
        if (title.showTitle != NULL)
        {
            [newArrayTitle addObject:title.showTitle];
        }
    }

    // getting the currently playing show on the channel
    for (int i = 0; i < [newArrayTitle count] - 1; i++)
    {
        if (([[newArrayBegin objectAtIndex:i+1] floatValue] < value) && (value < [[newArrayEnd objectAtIndex:i+2] floatValue]))
        {
            cell.currentlyPlaying.text = [newArrayTitle objectAtIndex:i+1];
        }
    }
    return cell;
}

有两个字 - 它是TV Guide应用程序,每个单元格都有频道徽标,频道名称和当前播放节目的两个标签。我将图像存储到我的项目中并从plist加载它们。当前正在播放的节目的数据是从11个xml文件中解析出来的。我很确定有更好的方法来做这个操作,但我是新手,我不知道如何提高我的滚动速度。我猜在cellForRowAtIndexPath方法中有太多的操作,但我不知道其他方式。有任何想法吗?提前谢谢!

这是我的应用中的一个单元格: enter image description here

0 个答案:

没有答案