【问题标题】:Load More Table View in ios在 ios 中加载更多表格视图
【发布时间】:2014-10-16 06:04:49
【问题描述】:

当用户从 Web 服务滚动表格视图时,我想加载数据。我的网络服务包含三页,但我只得到一页 JSON 数据。我的代码就像

在.h文件中

@property(strong,nonatomic)IBOutlet UITableView *table;
@property(strong,nonatomic)NSArray *imagesa;
@property(strong,nonatomic)IBOutlet UIActivityIndicatorView *spinner;

在.m文件中先用url定义两个宏队列

#define kBgQueue dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT,0)
#define imgURL [NSURL URLWithString:@"http://www.truemanindiamagazine.com/webservice/news.php"]

然后查看为

- (void)viewDidLoad
{
[super viewDidLoad];
dispatch_async(kBgQueue, ^{
    jdata = [NSData dataWithContentsOfURL: imgURL];
    [self performSelectorOnMainThread:@selector(fetchedData:) withObject:jdata waitUntilDone:YES];
});
self.table.pagingEnabled=YES;
[self.table reloadData];

-(void)fetchedData:(NSData *)responsedata
{
NSError* error;
NSDictionary *json = [NSJSONSerialization JSONObjectWithData:responsedata options:kNilOptions error:&error];
self.imagesa=[json objectForKey:@"data"];
if (self.imagesa.count)
{
    dispatch_async(dispatch_get_main_queue(), ^{
        [self.table reloadData];
    });
}
NSLog(@"images,%@",self.imagesa);
}
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return 1;
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return self.imagesa.count;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
   static NSString *cellidentifier=@"Cell";
CustumCell *cell=[tableView dequeueReusableCellWithIdentifier:cellidentifier];
if (cell == nil)
{
    NSArray *nib=[[NSBundle mainBundle]loadNibNamed:@"CustumCell" owner:self options:nil];
    cell=[nib objectAtIndex:0];
}

NSDictionary *dict = [self.imagesa objectAtIndex:indexPath.row];
NSString *img2=[dict valueForKey:@"post_image"];
[cell.photoimage sd_setImageWithURL:[NSURL URLWithString:[img2 stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]] placeholderImage:[UIImage imageNamed:@"Hisoka.jpg"] options:SDWebImageProgressiveDownload completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, NSURL *imageURL) {
    dispatch_async(dispatch_get_main_queue(), ^{
        NSLog(@"downloaded");

    });
}];
NSString *name=[dict valueForKey:@"post_title"];
cell.namelabel.text=name;
NSString *des=[dict valueForKey:@"post_content"];
cell.deslabel.text=des;


NSDateFormatter * dateFormatter = [[NSDateFormatter alloc]init];
[dateFormatter setDateFormat:@"yyyy-MM-dd hh:mm:ss"];
 NSString *date=[dict valueForKey:@"post_date"];
NSDate * dateNotFormatted = [dateFormatter dateFromString:date];
[dateFormatter setDateFormat:@"d-MMM-YYYY"];
NSString * dateFormatted = [dateFormatter stringFromDate:dateNotFormatted];
NSLog(@"Date %@",dateFormatted);
cell.datelabel.text=dateFormatted;
[self.spinner stopAnimating];
self.spinner.hidesWhenStopped=YES;

if (indexPath.row == [self.imagesa count] - 1)
{
    [self.table reloadData];
}
return cell;
}

我如何在表格视图中获取分页并将更多数据加载到表格视图中,就像在 android 中加载更多列表视图一样。

【问题讨论】:

    标签: ios json uitableview


    【解决方案1】:

    您必须再次调用服务并增加页码。我没有看到您为获取数据而传递的任何 pagenumber 参数。也许如果您的服务包含 3 个页面,那么您的服务必须具有 pagenumber 参数,以便您可以按页面获取数据。

    -(void)scrollViewDidScroll:(UIScrollView *)scrollView
    {
        CGFloat offsetY = scrollView.contentOffset.y;
        CGFloat contentHeight = scrollView.contentSize.height;
        if (offsetY > contentHeight - scrollView.frame.size.height)
        {
           // when your table is at last cell then increase your pagenumber and call service again and send increased pagenumber. 
           pageNum = pageNum + 1;
           [self getData];
        }
    }
    
    -(void)getData
    {
         dispatch_async(kBgQueue, ^{
              jdata = [NSData dataWithContentsOfURL: imgURL];
              [self performSelectorOnMainThread:@selector(fetchedData:) withObject:jdata waitUntilDone:YES];
         });
    }
    

    也许这会对你有所帮助。

    【讨论】:

    • 它工作正常,但只下载相同的页面数据而不是发送页面数据。我的网址有什么问题???我将 pageNum 定义为 NSIntiger 并给出其默认值为 0 是对的????
    猜你喜欢
    • 2011-08-19
    • 2015-09-30
    • 1970-01-01
    • 2012-07-02
    • 2018-07-02
    • 1970-01-01
    • 2016-05-14
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多