【问题标题】:How to load elements of table view in background and refresh it, when they have downloaded?下载后如何在后台加载表格视图的元素并刷新它?
【发布时间】:2014-07-04 12:32:19
【问题描述】:

在我的应用程序中,我有一个表格视图,每个单元格中都有一个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];
}

如何使我的表格视图动态化?

【问题讨论】:

    标签: ios objective-c xcode core-data


    【解决方案1】:

    由于您使用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];
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2012-05-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-03-05
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多