iOS开发关于UITableView从网络中获取到数据源,却在UITableView中不显示的问题
来源:互联网 发布:c语言可以用来做什么 编辑:程序博客网 时间:2024/05/22 17:16
大神就不用看了,对你来说肯定没什么营养,只针对新手朋友!!!
相信在iOS开发中大家会经常的应用到UITableView控件,于此同时我们也少不了与网络打交道,那么问题就产生了:
为什么我从网络获取到了数据源,但是却在UITableView不能成功显示出来?
下面,我用一个例子来给大家展示
首先这是我自己定义的一个类
- #import"SecondViewController.h"
- @interfaceSecondViewController ()
- @propertyNSMutableData *mutableData;
- @propertyNSString *string;
- @propertyNSMutableArray *courses;
- @end
- @implementation SecondViewController
- - (void)viewDidLoad {
- NSString *path =@"这是URL地址";
- NSURL *url = [NSURLURLWithString:path];
- NSURLRequest *request = [NSURLRequestrequestWithURL:url];
- NSURLConnection *connection = [NSURLConnectionconnectionWithRequest:requestdelegate:self];
- [connection start];
- [superviewDidLoad];
- self.classBarItem.badgeValue = nil;
- }
- - (void)didReceiveMemoryWarning {
- [superdidReceiveMemoryWarning];
- // Dispose of any resources that can be recreated.
- }
- - (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response{
- NSLog(@"%@", response);
- }
- - (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data{
- if(_mutableData == nil){
- _mutableData = [[NSMutableDataalloc]init];
- }
- [_mutableDataappendData:data];
- }
- - (void)connectionDidFinishLoading:(NSURLConnection *)connection{
- _string = [[NSStringalloc]initWithBytes:[_mutableDatabytes]length:[_mutableDatalength]encoding:NSUTF8StringEncoding];
- NSJSONSerialization *json_courses = [NSJSONSerializationJSONObjectWithData:_mutableDataoptions:0error:nil];
- NSMutableDictionary *json = [NSJSONSerializationJSONObjectWithData:_mutableDataoptions:0error:nil];
- _courses = [[NSMutableArrayalloc]init];
- for(int i = 0; i < [jsoncount]; i++){
- NSString *tag =@"course";
- NSString *str_i = [[NSStringalloc]initWithFormat:@"%i",i];
- tag = [tag stringByAppendingString:str_i];
- NSJSONSerialization *json_course = [json_coursesvalueForKey:tag];
- NSString *name = [json_coursevalueForKey:@"courseName"];
- NSLog(@"%@",name);
- [_coursesaddObject:name];
- }
- NSLog(@"%@",_courses);
- //获取NSMuTableArray中第n个元素的值
- //NSLog(@"%@", [_courses objectAtIndex:1]);
- _mutableData =nil;
- }
- - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
- return1;
- }
- - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
- NSLog(@"Array count is %i",[_coursescount]);
- return [_coursescount];
- }
- - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
- UITableViewCell *cell = [tableViewdequeueReusableCellWithIdentifier:@"courses"forIndexPath:indexPath];
- cell.textLabel.text = [_coursesobjectAtIndex:[indexPathrow]];
- // Configure the cell...
- return cell;
- }
- /*
- // Override to support conditional editing of the table view.
- - (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {
- // Return NO if you do not want the specified item to be editable.
- return YES;
- }
- */
- /*
- // Override to support editing the table view.
- - (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
- if (editingStyle == UITableViewCellEditingStyleDelete) {
- // Delete the row from the data source
- [tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];
- } else if (editingStyle == UITableViewCellEditingStyleInsert) {
- // Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view
- }
- }
- */
- /*
- // Override to support rearranging the table view.
- - (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)fromIndexPath toIndexPath:(NSIndexPath *)toIndexPath {
- }
- */
- /*
- // Override to support conditional rearranging of the table view.
- - (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath {
- // Return NO if you do not want the item to be re-orderable.
- return YES;
- }
- */
- /*
- #pragma mark - Navigation
- // In a storyboard-based application, you will often want to do a little preparation before navigation
- - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
- // Get the new view controller using [segue destinationViewController].
- // Pass the selected object to the new view controller.
- }
- */
- @end
程序成功获取到了数据源:
但是,问题就出现了,大家注意到没有,我用来存放网络读取的数据的NSMutableArray的count值为0,但是我也成功获取到了网络数据。
首先,分析,程序先执行输出了盛放我所需数据的长段,然后才从网络获取数据,那么我们就可以推断出,程序必然是先创建了UITableView,之后才获取到了网络数据。
问题产生的原因:程序在获取网络资源时并不会在主线程当中进行,所以程序会先执行之后的代码,然后再来获取网络资源
那么解决这个问题的方法也就明了了,在获取到网络资源后我们需要把它重新加载到tableView上
此时我们只需要在connectionDidFinishLoading末尾添加[self.tableViewreloadData];即可
OK,大功告成!
运行结果如下:
阅读全文
0 0
- iOS开发关于UITableView从网络中获取到数据源,却在UITableView中不显示的问题
- 在iOS中UITableView控件不显示能容
- iOS开发-简述UITableView中cell的重用问题
- 在UITableView中显示数据
- 关于UITableView 中两个困惑的问题
- UITableview在iOS11中出现的问题
- iOS UItableView显示问题
- [iOS 技术]相对布局 获取 UITableView中内容的高度,设置UITableView中内容的高度与UITableView高度相同
- iOS关于在UITableView中,实现多个cell中不同的倒计时实现
- iOS 关于在UITableView中,实现多个cell中不同的倒计时实现
- iOS关于在UITableView中,实现多个cell中不同的倒计时实现
- iOS中UITableView的cell点击事件不触发didSelectRowAtIndexPath
- iOS中UITableView的cell点击事件不触发didSelectRowAtIndexPath
- iOS 解决NSTimer在UITableView滑动中暂停问题
- iOS开发-关于UITableView去掉粘性的问题
- 关于在 UITableView FooterView 中控件宽度设置问题
- iOS开发中去掉UITableView中多余的分割线
- ios中封装网络和UITableView的综合运用
- C#学习总结(3)——代码连接mysql数据库(VS工具)
- 管理hiberfil.sys文件与pagefile.sys文件释放C盘
- 1024. 科学计数法 (20)
- 质量管理
- 09 C++Reference引用的用法(学自Boolean)
- iOS开发关于UITableView从网络中获取到数据源,却在UITableView中不显示的问题
- 类的加载、连接与初始化过程的详细分析(中)
- 第11周项目4
- VMware ESXI 开启虚拟机识别磁盘SCSI ID
- 如何测试网站的性能
- Java内部类小结
- smtplib.SMTPDataError: (554, 'DT:SPM 126 smtp5错误解决办法
- 非root用户执行程序---sudo的使用
- 括号配对(用栈实现)