Ios 在不重载的情况下将JSON api连接到tableView
我创建了一个简单的API,可以从数据库中检索所有新闻。然而,我不清楚在不占用太多空间的情况下检索所有内容的最佳实践是什么?这个数据库中有大量的新闻,我想如果它检索所有新闻并在tableView中显示它们,对用户来说是不好的。所以当你滚动到按钮的时候,我想我需要给我的tableView增加一些加载功能 到目前为止,我刚刚制作了这个函数,它可以检索所有的新闻,但是为了优化它,我的第一步是什么呢Ios 在不重载的情况下将JSON api连接到tableView,ios,objective-c,json,uitableview,swift,Ios,Objective C,Json,Uitableview,Swift,我创建了一个简单的API,可以从数据库中检索所有新闻。然而,我不清楚在不占用太多空间的情况下检索所有内容的最佳实践是什么?这个数据库中有大量的新闻,我想如果它检索所有新闻并在tableView中显示它们,对用户来说是不好的。所以当你滚动到按钮的时候,我想我需要给我的tableView增加一些加载功能 到目前为止,我刚刚制作了这个函数,它可以检索所有的新闻,但是为了优化它,我的第一步是什么呢 func getRecent(url: NSString) { let recentUrl =
func getRecent(url: NSString) {
let recentUrl = NSURL(string: url as String)
var recentRequest = NSURLRequest(URL: recentUrl!)
var recentData = NSURLConnection.sendSynchronousRequest(recentRequest, returningResponse: nil, error: nil)
let jsonArray = JSON(data: recentData!)
for (key: String, subJson: JSON) in jsonArray {
// Create an object and parse your JSON one by one to append it to your array
var newNewsObject = News(id: subJson["id"].intValue, title: subJson["title"].stringValue, link: subJson["url"].stringValue, imageLink: subJson["image_url"].stringValue, summary: subJson["news_text"].stringValue, date: getDate(subJson["date"].stringValue))
recentArray.append(newNewsObject)
}
arrayNews.append(recentArray)
}
创建一个函数,如果可能的话,在某些分页中响应新闻[可选] 您可以使一个表视图成为您想要的分页大小,比如每次发布10条新闻,然后是分页,然后您可以选择这样的方式
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return [newsArray count]+1;
}
- (UITableViewCell *)tableView:(UITableView *)tableView
cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *MyIdentifier = @"MyIdentifier";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:MyIdentifier];
if (indexPath.row< newsArray.cout) {
if (cell == nil)
{
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"MyIdentifier"];
}
}
else {
Cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"LoadingViewCell"];
// add 10 more newsArray to the Array.
}
}
-(NSInteger)节数表视图:(UITableView*)表视图
{
返回1;
}
-(NSInteger)表视图:(UITableView*)表视图行数节:(NSInteger)节{
返回[新闻数组计数]+1;
}
-(UITableView单元格*)表格视图:(UITableView*)表格视图
cellForRowAtIndexPath:(NSIndexPath*)indexPath
{
静态NSString*MyIdentifier=@“MyIdentifier”;
UITableViewCell*单元格=[tableView dequeueReusableCellWithIdentifier:MyIdentifier];
if(indexath.row
希望这能帮助到小学
享受编码的乐趣