0

我有以下问题:我的 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;
}

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

这是我的应用程序中的一个单元格: 在此处输入图像描述

4

0 回答 0