Ios 如何在objective c中异步加载UITableview滚动和核心数据获取数据
我的应用程序中有核心数据库。在该数据库中,一个模型课程事件包含超过250000条记录,每条记录大约有5个字段 每个记录一个UITableViewCell的值 但我不想在一个获取请求中获取所有记录。要根据UITableView滚动条获取大约N条记录 例如: 当首次加载表视图时,如果用户希望获取20条记录,则每当用户滚动UITableView时,都需要获取下一条20条记录,比如需要根据UITableView的滚动从模型中获取数据 我怎样才能做到这一点。请帮助….试试这个:Ios 如何在objective c中异步加载UITableview滚动和核心数据获取数据,ios,objective-c,iphone,core-data,Ios,Objective C,Iphone,Core Data,我的应用程序中有核心数据库。在该数据库中,一个模型课程事件包含超过250000条记录,每条记录大约有5个字段 每个记录一个UITableViewCell的值 但我不想在一个获取请求中获取所有记录。要根据UITableView滚动条获取大约N条记录 例如: 当首次加载表视图时,如果用户希望获取20条记录,则每当用户滚动UITableView时,都需要获取下一条20条记录,比如需要根据UITableView的滚动从模型中获取数据 我怎样才能做到这一点。请帮助….试试这个: -(void)tableV
-(void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath{
if (indexPath.row == arrObject.count-1) {
if (arrObject.count < totalDataCount) {
currentPage++; // initial value is zero
[self getDataFromResource]; // call api here
}
}
}
-(void)tableView:(UITableView*)tableView将显示单元格:(UITableViewCell*)用于rowatindexpath的单元格:(nsindepath*)indepath{
if(indexath.row==arrObject.count-1){
if(arobject.count
//使用谓词,您可以根据滚动从核心数据中获取20条记录
-(void)viewDidLoad
{
[super viewDidLoad];
UIRefreshControl *refreshControl;
[refreshControl addTarget:self action:@selector(refreshTable) forControlEvents:UIControlEventValueChanged];
minId = 0;
maxId = 20;
maxId = minId +maxId;
NSPredicate*preicate = [NSPredicate predicateWithFormat:@"id == %d && id < %d",minId,maxId];// id is your unique id in core data.
// Fetch first 20 records from core data using predicate.
}
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
{
float bottomEdge = scrollView.contentOffset.y + scrollView.frame.size.height;
if (bottomEdge >= self.tableView.contentSize.height) //this will get last record of tableview when scroll
{
minId = maxId;
maxId = minId + maxId;
NSPredicate*preicate = [NSPredicate predicateWithFormat:@"id == %d && id < %d",minId,maxId];// id is your unique id in core data.
// Fetch next 20 records from core data using predicate.
[self.tableView reloadData];
}
}
// Whenever scroll down ,refresh method is called
- (void)refreshTable {
[self.yourArray removeAllobjects];
minId = 0;
maxId = 20;
maxId = minId +maxId;
NSPredicate*preicate = [NSPredicate predicateWithFormat:@"id == %d && id < %d",minId,maxId];// id is your unique id in core data.
// Fetch first 20 records from core data using predicate.
[self.tableView reloadData]
}
-(void)viewDidLoad
{
[超级视图下载];
UIRefreshControl*refreshControl;
[refreshControl添加目标:自我操作:@selector(refreshTable)forControlEvents:UIControlEventValueChanged];
minId=0;
maxId=20;
maxId=minId+maxId;
NSPredicate*证书=[NSPredicate PREDITEWITHFORMAT:@“id==%d&&id<%d”,minId,maxId];//id是核心数据中的唯一id。
//使用谓词从核心数据中获取前20条记录。
}
-(无效)ScrollViewDiEndDecelling:(UIScrollView*)scrollView
{
浮动底边=scrollView.contentOffset.y+scrollView.frame.size.height;
if(bottomEdge>=self.tableView.contentSize.height)//这将在滚动时获取tableView的最后一条记录
{
minId=maxId;
maxId=minId+maxId;
NSPredicate*证书=[NSPredicate PREDITEWITHFORMAT:@“id==%d&&id<%d”,minId,maxId];//id是核心数据中的唯一id。
//使用谓词从核心数据中获取接下来的20条记录。
[self.tableView重载数据];
}
}
//每当向下滚动时,就会调用刷新方法
-(空)刷新表{
[self.yourArray removeAllobjects];
minId=0;
maxId=20;
maxId=minId+maxId;
NSPredicate*证书=[NSPredicate PREDITEWITHFORMAT:@“id==%d&&id<%d”,minId,maxId];//id是核心数据中的唯一id。
//使用谓词从核心数据中获取前20条记录。
[self.tableView重新加载数据]
}
@satya murty
试试这个:-
uitableviewcell异步使用核心数据
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
cell.tag = indexPath.row;
//UserRecords TableName
//self.yourArray records array (fetch from core data)
UserRecords*records = self.yourArray[indexPath.row];
if (records)
{
cell.imageView.image = nil;
dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH, 0ul);
dispatch_async(queue, ^(void) {
NSData *imageData = [NSData dataWithContentsOfURL:[NSURL URLWithString:records.imageURL];
UIImage* image = [[UIImage alloc] initWithData:imageData];
if (image) {
dispatch_async(dispatch_get_main_queue(), ^{
if (cell.tag == indexPath.row) {
cell.imageView.image = image;
[cell setNeedsLayout];
}
});
}
});
cell.textLabel.text = records.title;
}
return cell;
}
谢谢你的回复。。。你能给我完整的密码吗。我想在首次加载表视图时获取20条记录,每当用户滚动UITableView需要获取下一条20条记录时,比如需要根据UITableview的滚动从模型中获取数据。你从api中获取数据吗?@KKRocks.是的,我从api中获取数据..我想要这样..我关心的是,当加载tableview时,它应该先获取200(例如),当用户向上滚动到150单元格时,然后需要获取下一个200(201-400),如果用户再次向下滚动,需要获取以前的200(1-200)。就像我的抓取应该与UITableView可见单元格同步一样。然后您需要在服务器端设置分页代码。您只需要传递页码。我的疑问是,例如,设备中的网络不可用。所以设备处于脱机模式。在脱机模式下,我想从核心数据获取数据。。所以在这个场景中,如何实现这一点。请指导我您的数据是如何排序的?数据是否有可预测的分布?(即按数据排序,每小时有一个条目?@JonRose感谢您的回复.)。。我是这样做的,我关心的是当加载tableview时,它应该先获取200(例如),当用户向上滚动到150个单元格时,它需要获取下一个200(201-400),如果用户再次向下滚动,则需要获取前一个200(1-200)。我的提取应该与UITableView可见单元格同步。你能给我发送完整的代码吗。。例如,我有标题、图片和核心数据。我想在加载tableview时,它应该先获取200(例如),当用户向上滚动到150个单元格时,它需要获取下一个200(201-400),如果用户再次向下滚动,则需要获取上一个200(1-200)。就像我的抓取应该与UITableView同步一样,非常感谢。。bro非常感谢..实际上我想要一个小例子,比如使用核心数据在uitableviewcell中异步显示标题和图像。。请将相关代码发送给我……这是另一个问题,你应该提出一个新问题。@satyamurty如果对你有用,请投+1票。谢谢你的回复。但实际上我想要这样..比如我有标题、图片和核心数据。我想在加载tableview时,它应该先获取200(例如),当用户向上滚动到150个单元格时,它需要获取下一个200(201-400),如果用户再次向下滚动,则需要获取上一个200(1-200)。就像我的抓取应该与UITableView可见单元格同步一样
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
cell.tag = indexPath.row;
//UserRecords TableName
//self.yourArray records array (fetch from core data)
UserRecords*records = self.yourArray[indexPath.row];
if (records)
{
cell.imageView.image = nil;
dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH, 0ul);
dispatch_async(queue, ^(void) {
NSData *imageData = [NSData dataWithContentsOfURL:[NSURL URLWithString:records.imageURL];
UIImage* image = [[UIImage alloc] initWithData:imageData];
if (image) {
dispatch_async(dispatch_get_main_queue(), ^{
if (cell.tag == indexPath.row) {
cell.imageView.image = image;
[cell setNeedsLayout];
}
});
}
});
cell.textLabel.text = records.title;
}
return cell;
}