如何在后台加载表格视图的元素并在下载后刷新它?

时间:2014-07-04 12:32:20

标签: ios objective-c xcode core-data

在我的应用程序中,我有一个表视图,每个单元格中都有UIImageView。它带来了互联网上的图片,当然我想在我的应用程序中使用它,而不是等到所有图片都被加载并保存在CoreData中。我想首先显示文本信息,并在准备好时显示图片。

首先是tableView代表:

-(NSInteger) numberOfSectionsInTableView:(UITableView *)tableView{
return maxSerials;
//return [[self.fetchedResultsController fetchedObjects] count];
}

 -(NSInteger) tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
return 1;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return 30.0f;
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
UIView *customView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, 30)];
customView.backgroundColor = [UIColor colorWithWhite:224.0/255.0 alpha:0];

UIView *lineView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, customView.frame.size.width, 30)];
lineView.backgroundColor = [UIColor colorWithWhite:224.0/255.0 alpha:0.90];
[customView addSubview:lineView];

UILabel *sectionHeader = [[UILabel alloc] initWithFrame:customView.frame];
sectionHeader.backgroundColor = [UIColor clearColor];
sectionHeader.textAlignment = NSTextAlignmentCenter;
sectionHeader.font = [UIFont boldSystemFontOfSize:16];
sectionHeader.textColor = [UIColor colorWithWhite:0 alpha:1];
sectionHeader.text = [[serialsAnswer objectAtIndex: section] objectForKey: @"name"];
[customView addSubview:sectionHeader];

return customView;
}

-(UITableViewCell *) tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault
                                               reuseIdentifier:@"serialCell"];
NSLog(@"cell");

Serial *serial = [self.fetchedResultsController.fetchedObjects objectAtIndex:indexPath.section];

UIImage *poster = [[UIImage alloc] initWithData: serial.poster];
UIImageView *imageView = [[UIImageView alloc] initWithFrame: CGRectMake(0, 0, 160, 232)];
imageView.image = poster;
[cell addSubview:imageView];

UILabel *views = [[UILabel alloc] initWithFrame: CGRectMake(170, 22, 140, 15)];
views.textAlignment = NSTextAlignmentCenter;
views.text = @"Просмотров:";

UILabel *views2 = [[UILabel alloc] initWithFrame: CGRectMake(170, 42, 140, 15)];
views2.textAlignment = NSTextAlignmentCenter;
views2.text = [[serialsAnswer objectAtIndex: indexPath.section] objectForKey: @"views"];

UILabel *IMDB = [[UILabel alloc] initWithFrame: CGRectMake(170, 99, 140, 15)];
IMDB.textAlignment = NSTextAlignmentCenter;
IMDB.text = @"IMDB:";

UILabel *IMDB2 = [[UILabel alloc] initWithFrame: CGRectMake(170, 119, 140, 15)];
IMDB2.textAlignment = NSTextAlignmentCenter;
IMDB2.text = [[serialsAnswer objectAtIndex: indexPath.section] objectForKey: @"imdb_rating"];

UILabel *year = [[UILabel alloc] initWithFrame: CGRectMake(170, 176, 140, 15)];
year.textAlignment = NSTextAlignmentCenter;
year.text = @"Год:";

UILabel *year2 = [[UILabel alloc] initWithFrame: CGRectMake(170, 196, 140, 15)];
year2.textAlignment = NSTextAlignmentCenter;
year2.text = [[serialsAnswer objectAtIndex: indexPath.section] objectForKey: @"year"];

[cell addSubview: year];
[cell addSubview: year2];
[cell addSubview: IMDB];
[cell addSubview: IMDB2];
[cell addSubview: views];
[cell addSubview: views2];

return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 240.0f;
}

-(void) tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
[tableView deselectRowAtIndexPath:indexPath animated:true];

[self performSegueWithIdentifier: @"serialsToSerial" sender: indexPath];
}

- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath
{
if (indexPath.section == maxSerials - 1)
{
    maxSerials +=10;

    [self downloadCells];
}
}

这是保存方法:

- (void) saveSerial:(NSDictionary *) newSerial
{
id path = [[NSString alloc] initWithFormat:@"http://sakh.tv/%@", [newSerial objectForKey: @"poster"]];
NSURL *url = [NSURL URLWithString:path];
NSData *data = [NSData dataWithContentsOfURL:url];
_serial.poster = data;

_serial = [NSEntityDescription insertNewObjectForEntityForName:@"Serial" inManagedObjectContext:[self managedObjectContext]];
_serial.serial_id = [newSerial objectForKey:@"id"];

_serial.views = [[NSNumber alloc] initWithInt: [[newSerial objectForKey:@"views"] integerValue]];
_serial.name = [newSerial objectForKey:@"name"];

[super saveToCoreData];
}

如何让我的表视图动态化?

1 个答案:

答案 0 :(得分:0)

由于您正在使用fetchedResultController表格,请实施(void)controllerDidChangeContent:(NSFetchedResultsController *)controller并通过调用[self.tableView reloadData]重新加载表格视图

顺便说一句,看起来你的子类不是tableViewCell,考虑一下。 而不是使用:

UITableViewCell *cell = [[UITableViewCell alloc]
    initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"serialCell"];
Serial *serial = [self.fetchedResultsController.fetchedObjects
    objectAtIndex:indexPath.section];

尝试:

MyCell *aCell = (MyCell *) [tableView 
    dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];