![](/img/trans.png)
[英]How to add one UIButton on Row and Section of UITableview in Objective-C
[英]Objective-c: how to properly add more rows to a UITableView
我知道與這個問題有很多類似的問題,但是我還沒有找到任何可以解釋的問題。
我有一個UITableView
從URL獲取其內容(即JSON),我正在使用此方法來獲取JSON數據:
-(void)getContents
{
NSString *contentStartString = [NSString stringWithFormat:@"%ld",(long)contentStart];
NSString *contentCountString = [NSString stringWithFormat:@"%ld",(long)contentCount];
NSString *contentsUrl = @"http://www.ana.fm/api/index.php?start=";
contentsUrl = [contentsUrl stringByAppendingString:contentStartString];
contentsUrl = [contentsUrl stringByAppendingString:@"&count="];
contentsUrl = [contentsUrl stringByAppendingString:contentCountString];
NSLog(@"%@",contentsUrl);
NSURL *URL = [NSURL URLWithString:contentsUrl];
NSURLRequest *request = [NSURLRequest requestWithURL:URL];
//AFNetworking asynchronous url request
AFHTTPRequestOperation *operation = [[AFHTTPRequestOperation alloc]
initWithRequest:request];
operation.responseSerializer = [AFJSONResponseSerializer serializer];
[operation setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject) {
NSLog(@"%@", responseObject);
jsonContents = [responseObject objectForKey:@"contents"];
[self.tableView reloadData];
tableLoadMoreCapability = true;
} failure:nil];
[operation start];
}
這可以完美地工作,但是當我到達表格的末尾時,可以使用此方法檢測到:
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
{
float endScrolling = scrollView.contentOffset.y + scrollView.frame.size.height;
if (endScrolling >= scrollView.contentSize.height)
{
if(tableLoadMoreCapability == true){
contentStart = contentStart + 20;
[self updateContentsTable];
}
}
}
如您所見,我想執行此方法updateContentsTable
(到達表末尾):
- (void)updateContentsTable
{
tableLoadMoreCapability = false;
NSLog(@"load more rows");
NSString *contentStartString = [NSString stringWithFormat:@"%ld",(long)contentStart];
NSString *contentCountString = [NSString stringWithFormat:@"%ld",(long)contentCount];
NSString *contentsUrl = @"http://www.ana.fm/api/index.php?start=";
contentsUrl = [contentsUrl stringByAppendingString:contentStartString];
contentsUrl = [contentsUrl stringByAppendingString:@"&count="];
contentsUrl = [contentsUrl stringByAppendingString:contentCountString];
NSLog(@"%@",contentsUrl);
NSURL *URL = [NSURL URLWithString:contentsUrl];
NSURLRequest *request = [NSURLRequest requestWithURL:URL];
//AFNetworking asynchronous url request
AFHTTPRequestOperation *operation = [[AFHTTPRequestOperation alloc]
initWithRequest:request];
operation.responseSerializer = [AFJSONResponseSerializer serializer];
[operation setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject) {
NSLog(@"%@", responseObject);
jsonContents = [responseObject objectForKey:@"contents"];
[self.tableView reloadData];
} failure:nil];
[operation start];
}
現在我正在使用[self.tableView reloadData];
因此,代碼可以正常工作,並且表中重新加載了新數據,我不希望那樣,我需要在舊行下面插入新行,我從其他答案中學到了必須使用[self.tableView insertRowsAtIndexPaths: .....]
但我不知道該怎么做。
任何人都可以澄清該怎么做?
提前致謝。
我認為您要做的工作超出了您的需要。 為什么不告訴表視圖您擁有的行數大於第一次調用時獲得的行數? 然后,一旦委托被要求在您實際加載的單元格之外連續顯示一個單元格(表明用戶已滾動瀏覽了您已有的數據),您便可以通過JSON獲得下一批數據。 通過委托方法,您可以精確控制可見單元格中顯示的數據。 我會完全避免滾動的東西。
您唯一需要注意的是何時(以及如何)重新加載表:要避免重新加載調用reload調用reload...。
可以將表的行視為一個在所有行的(虛擬)表上滑動的窗口。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.