如何在不使用 Api 的情况下在 uitableview 中进行分页?
Posted
技术标签:
【中文标题】如何在不使用 Api 的情况下在 uitableview 中进行分页?【英文标题】:How to do pagination in uitableview without using Api? 【发布时间】:2015-10-19 11:45:41 【问题描述】:我正在做一个项目,我想在 uitableview
中使用分页而不使用 api。我有一个nsdictionary
,我把那个字典保存在一个nsarray
,现在我希望在列表视图类型中有10,20,30个数字,我选择哪个数字rows
将显示在UI中.
我已经在uitableview
中完成了这项工作
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
NSString *string1 = @"cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:string1];
if(!cell)
cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:string1];
UILabel *label1 = (UILabel*)[cell.contentView viewWithTag:401];
CGFloat widthLabelHeader = CGRectGetWidth(orderTable.frame)/7;
if(!label1)
label1 = [[UILabel alloc]init];
label1.frame = CGRectMake(0, 0, widthLabelHeader, 45);
label1.layer.borderWidth = 0.5;
label1.font = [UIFont fontWithName:@"ChalkboardSE-Bold" size:14];
[cell.contentView addSubview:label1];
label1.textAlignment = NSTextAlignmentCenter;
UILabel *label2 = (UILabel*)[cell.contentView viewWithTag:402];
if(!label2)
label2 = [[UILabel alloc]init];
label2.frame = CGRectMake(CGRectGetMaxX(label1.frame), 0, widthLabelHeader, 45);
label2.layer.borderWidth = 0.5;
label2.font = [UIFont fontWithName:@"ChalkboardSE-Bold" size:14];
[cell.contentView addSubview:label2];
label2.textAlignment = NSTextAlignmentCenter;
UILabel *label3 = (UILabel*)[cell.contentView viewWithTag:403];
if(!label3)
label3 = [[UILabel alloc]init];
label3.frame = CGRectMake(CGRectGetMaxX(label2.frame), 0, widthLabelHeader, 45);
label3.layer.borderWidth = 0.5;
label3.font = [UIFont fontWithName:@"ChalkboardSE-Bold" size:14];
[cell.contentView addSubview:label3];
label3.textAlignment = NSTextAlignmentCenter;
UILabel *label4 = (UILabel*)[cell.contentView viewWithTag:404];
if(!label4)
label4 = [[UILabel alloc]init];
label4.frame = CGRectMake(CGRectGetMaxX(label3.frame), 0, widthLabelHeader, 45);
label4.layer.borderWidth = 0.5;
label4.font = [UIFont fontWithName:@"ChalkboardSE-Bold" size:14];
[cell.contentView addSubview:label4];
label4.textAlignment = NSTextAlignmentCenter;
UILabel *label5 = (UILabel*)[cell.contentView viewWithTag:405];
if(!label5)
label5 = [[UILabel alloc]init];
label5.frame = CGRectMake(CGRectGetMaxX(label4.frame), 0, widthLabelHeader, 45);
label5.layer.borderWidth = 0.5;
label5.font = [UIFont fontWithName:@"ChalkboardSE-Bold" size:14];
[cell.contentView addSubview:label5];
label5.textAlignment = NSTextAlignmentCenter;
UILabel *label6 = (UILabel*)[cell.contentView viewWithTag:406];
if(!label6)
label6 = [[UILabel alloc]init];
label6.frame = CGRectMake(CGRectGetMaxX(label5.frame), 0, widthLabelHeader, 45);
label6.layer.borderWidth = 0.5;
label6.font = [UIFont fontWithName:@"ChalkboardSE-Bold" size:14];
[cell.contentView addSubview:label6];
label6.textAlignment = NSTextAlignmentCenter;
UILabel *label7 = (UILabel*)[cell.contentView viewWithTag:407];
if(!label7)
label7 = [[UILabel alloc]init];
label7.frame = CGRectMake(CGRectGetMaxX(label6.frame), 0, widthLabelHeader, 45);
label7.layer.borderWidth = 0.5;
label7.font = [UIFont fontWithName:@"ChalkboardSE-Bold" size:14];
[cell.contentView addSubview:label7];
label7.textAlignment = NSTextAlignmentCenter;
dict1 = [array1 objectAtIndex:indexPath.row];
if( dict1)
label1.text = [dict1 objectForKey:@"key1" ];
label1.backgroundColor = [UIColor lightGrayColor];
label2.text = [dict1 objectForKey:@"key2" ];
label2.backgroundColor = [UIColor lightGrayColor];
label3.text = [dict1 objectForKey:@"key3" ];
label3.backgroundColor = [UIColor lightGrayColor];
label4.text = [dict1 objectForKey:@"key4" ];
label4.backgroundColor = [UIColor lightGrayColor];
label5.text = [dict1 objectForKey:@"key5" ];
label5.backgroundColor = [UIColor lightGrayColor];
label6.text = [dict1 objectForKey:@"key6" ];
label6.backgroundColor = [UIColor lightGrayColor];
label7.text = [dict1 objectForKey:@"key7" ];
label7.backgroundColor = [UIColor lightGrayColor];
return cell;
请帮我怎么做?
【问题讨论】:
【参考方案1】:使用此方法是方法并调用 Api 并附加到表数据源中使用的同一数组中
-(void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
BOOL endOfTable = (scrollView.contentOffset.y >= ((showListingArray.count * 60) - scrollView.frame.size.height)); // Here 40 is row height
nextPage ++;
NSString *nextPageStr = [NSString stringWithFormat:@"%d", nextPage];
int totalpages = [[PaginnationDic objectForKey:@"totalPages"]intValue];
if ( endOfTable && !scrollView.dragging && !scrollView.decelerating)
if (nextPage >= totalpages)
// NSLog(@"No More Page to load");
return;
objPCDetailTableView.tableFooterView = footerView;
NSDictionary *parametrs = @@"movieid":_movieID, @"page":nextPageStr, @"cityId":[Utility getCityID];
[self getMoviesDetailFromApi:parametrs];
[(UIActivityIndicatorView *)[footerView viewWithTag:10] startAnimating];
【讨论】:
【参考方案2】:你应该设置这个删除方法
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section;
使用您在文本字段中输入的号码并拨打此电话[tableView reloadData]
或使用此委托方法
- (void)insertRowsAtIndexPaths:(NSArray<NSIndexPath *> *)indexPaths withRowAnimation:(UITableViewRowAnimation)animation;
将新行插入到您的 tableView 中。
【讨论】:
以上是关于如何在不使用 Api 的情况下在 uitableview 中进行分页?的主要内容,如果未能解决你的问题,请参考以下文章
如何在不设置 lambda 的情况下在 apigateway http_proxy 上添加 CORS?
如何在不使用webView的情况下在Android应用程序中显示Web内容