当每个单元格上滚动表格时,视差uiviewcontroller卡住了

时间:2014-08-15 21:03:49

标签: ios objective-c uitableview ios7

我正在开发一个应用程序,它将在Parallax风格的UITableView中从服务器加载远程数据。并且它正在完美地加载数据,但是我无法无缝地滚动表格并且它在滚动时粘在视图中出现的每个单元格上。这是我的代码,表中只有10个条目。感谢

- (void)viewDidLoad
{
    [self hasInternet];
    self.tableView.dataSource = self;
    self.tableView.delegate = self;
    [self loadData];
    self.edgesForExtendedLayout=UIRectEdgeNone;
    self.extendedLayoutIncludesOpaqueBars=NO;
    self.automaticallyAdjustsScrollViewInsets=NO;
    [super viewDidLoad];
}
- (void)viewWillAppear:(BOOL)animated
{
    [self scrollViewDidScroll:nil];
    [super viewWillAppear:animated];
    [self loadData];
    self.tableView.dataSource = self;
    self.tableView.delegate = self;
}
- (void) loadData{
    name = @"name";
    email = @"email";
    thumbnail = @"thumbnail";
    myObject = [[NSMutableArray alloc] init];
    NSData *jsonSource = [NSData dataWithContentsOfURL:[NSURL URLWithString:@"http://URL.php"]];
    id jsonObjects = [NSJSONSerialization JSONObjectWithData:jsonSource options:NSJSONReadingMutableContainers error:nil];
    for (NSDictionary *dataDict in jsonObjects) {
        NSString *title_data = [dataDict objectForKey:@"fname"];
        NSString *title_data2 = [dataDict objectForKey:@"lname"];
        NSString *fulname = [NSString stringWithFormat:@"%@ %@", title_data, title_data2];
        NSString *emAil = [dataDict objectForKey:@"email"];
        NSString *thumbnail_data = [dataDict objectForKey:@"img"];
        thumbnail_data = [NSString stringWithFormat:@"http://URL/upload/%@",thumbnail_data];
        dictionary = [NSDictionary dictionaryWithObjectsAndKeys: fulname, name, emAil, email, thumbnail_data, thumbnail, nil];
        [myObject addObject:dictionary];
    }
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return myObject.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"parallaxCell";
    JBParallaxCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell=[[UITableViewCell alloc]initWithStyle:
              UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier];
    }
    NSDictionary *tmpDict = [myObject objectAtIndex:indexPath.row];
    NSMutableString *text;
    text = [NSMutableString stringWithFormat:@"%@",[tmpDict objectForKeyedSubscript:name]];
    NSMutableString *mail;
    mail = [NSMutableString stringWithFormat:@"%@",[tmpDict objectForKeyedSubscript:email]];
    NSMutableString *images;
    images = [NSMutableString stringWithFormat:@"%@ ",[tmpDict objectForKey:thumbnail]];
    NSURL *url = [NSURL URLWithString:[tmpDict objectForKey:thumbnail]];
    NSData *data = [NSData dataWithContentsOfURL:url];
    UIImage *img = [[UIImage alloc]initWithData:data];
    cell.titleLabel.text = [NSString stringWithFormat:@"%@",text];
    cell.subtitleLabel.text = [NSString stringWithFormat:@"%@",mail];
    cell.parallaxImage.image = img;
    return cell;
}
- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
    NSArray *visibleCells = [self.tableView visibleCells];    
    for (JBParallaxCell *cell in visibleCells) {
        [cell cellOnTableView:self.tableView didScrollOnView:self.view];
    }
}

1 个答案:

答案 0 :(得分:3)

看起来你有一个严重的性能问题,我想这是因为这段代码:

NSURL *url = [NSURL URLWithString:[tmpDict objectForKey:thumbnail]];
NSData *data = [NSData dataWithContentsOfURL:url];
UIImage *img = [[UIImage alloc]initWithData:data]; 

这应该在UI线程之外完成!一旦你将在后台下载数据并在完成提取时更新UI,那么滚动的问题就会消失。

对于延迟加载(下载)图像,您在github上有几个库(Nimbus或者AFNetworking库附带的UIImageView类别。)

我提出了一个presentation来提及像你这样的问题。

最简单的解决方案是(像这样但仍然考虑使用我提到的库来实现更好的效果,你可以设置一些占位符等):

dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{ 
    NSURL *url = [NSURL URLWithString:[tmpDict objectForKey:thumbnail]];
    NSData *data = [NSData dataWithContentsOfURL:url];
    dispatch_async(dispatch_get_main_queue(), ^{ 
         cell.parallaxImage.image = [[UIImage alloc]initWithData:data];
    }); 
});