ios – UITableView reloadData什么都不做(UITableView不是nil)

我正在构建我的第一个真正的应用程序,你们已经是最有帮助的,但我一直在努力与tableView不更新.我希望这里的某个人可以对此有所了解,并且可能想出任何关于我可能错过或完成不必要的其他事情的想法.

目前,应用应该执行以下操作:

>使用来自sqlite db的数据填充表(基本上只列出所有表)
>用户按下按钮(触发fetchData方法)
>(void)fetchData将本地数据库与服务器数据库校验和进行比较
>(void)fetchData设置NSURLConnection,通过POST与服务器通信
>当连接收到所有数据时,它会更新本地数据库
>使用新的db数据更新tableView dataSource(NSMutableArray)
> tableview更新并列出数据库中的所有表[myTableView reloadData]被调用

我经过双重检查的事情:tableview和delegate,datasource和outlet之间的连接; tableView不是零;数据源本身已经准确更新;

一切都达到了最后一点(7).似乎myTableView当时并不是零.想法?如果您需要查看更多我的代码,请告诉我.

我也看了一下beginUpdates和endUpdates的方法,但在我看来,他们一度关注一些变化和用户交互性.我想根据用户选择重新加载整个表(即希望根据当前用户登录反映整个其他SQL选择字符串).或者还有另一种更好的方法吗?

提前致谢!

这是一段相当不错的代码:

#import "FirstViewController.h"
@interface FirstViewController ()
@end

@implementation FirstViewController
#import "FirstViewController.h"

@synthesize myTextView,myTableViewDataSource,myFetchedData,resultat,tablesAndChecks,tablesArr,checksArr,tablesToRequest,receivedData,receivedDataString,SQL,sqlStatementsArr,failedSqlStatementsArr,failedSqlStatementsCodeArr,dbloop1;



#pragma mark Table view methods
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}

// Customize the number of rows in the table view.
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    NSLog(@"Konfigurerar tableView"); //swedish for "Configuring tableView"
    if(myTableView.dataSource==nil){
        NSLog(@"datasource = nil");
    }else{
        NSLog(@"datasource != nil"); //This prints in log
    }
    NSLog(@"%d",[myTableViewDataSource count]); //prints "19" in log

    return [myTableViewDataSource count];
}

// Customize the appearance of table view cells.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    NSLog(@"calling cellForRowAtIndexPath"); //This does NOT print in log

    static NSString *CellIdentifier = @"myCell";

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
    }

    // Set up the cell...
    cell.textLabel.font = [UIFont fontWithName:@"Helvetica" size:15];
    cell.textLabel.text = [NSString  stringWithFormat:@"Tabell %d: %@",[indexPath row],[myTableViewDataSource objectAtIndex:[indexPath row]]];

    return cell;
}



- (void)connectionDidFinishLoading:(NSURLConnection *)connection
{
    NSLog(@"connection did finish loading");

    //...
    //script that receives a long SQL-string from the server
    //and then updates the database with it goes here    
    //...

    //Ok,so now the database has updated correctly,and it's time 
    //to update the tableview so that it reflects the new data.
    //
    //Get data from database...
    NSMutableArray * tempArray = [[NSMutableArray alloc] initWithCapacity:0];

    if (sqlite3_open(dbpath,&contactDB) == SQLITE_OK)
    {
        NSLog(@"Databasen öppnad");
        NSString *beginSQL = [NSString stringWithFormat: @"SELECT name FROM sqlite_master WHERE type='table' ORDER BY name;"];
        const char *begin_stmt = [beginSQL UTF8String];
        sqlite3_prepare_v2(contactDB,begin_stmt,-1,&statement,NULL);
        while(sqlite3_step(statement) == SQLITE_ROW) {
            char *col1 = (char *)sqlite3_column_text(statement,0);
            if (col1 !=NULL){
                [tempArray addObject:[NSString stringWithUTF8String: col1]];
            }
        }
        if (sqlite3_step(statement) == SQLITE_DONE)
        {
            NSLog(@"#####%s;   Done: %@",sqlite3_errmsg(nil),beginSQL);
        } else {
            NSLog(@"#####%s;   Error with string: %@; Errcode: %d Errmsg: %s",beginSQL,sqlite3_errcode(contactDB),sqlite3_errmsg(nil));
        }

        NSLog(@"%d",sqlite3_finalize(statement));
        sqlite3_close(contactDB);

        //update the datasource to the values of tempArray
        myTableViewDataSource = tempArray;

        //Some logging to see that the updated data is in the array... (which it is)
        NSLog(@"myTableViewDataSource count: %d",[myTableViewDataSource count]);

        for (i=0; i<[myTableViewDataSource count]; i++) {
            NSLog(@"%@",[myTableViewDataSource objectAtIndex:i]);
        }
    }

    //Finally,check so that myTableView isn't nil and can receive messages
    if(myTableView == nil){
        NSLog(@"NILCHECK mytableview is nil!");
    }else{
        NSLog(@"NILCHECK mytableview is NOT nil!"); //This is printed out to the log
    }

    //Reload data -> nothing happens...
    [myTableView reloadData];
}


- (void)fetchData
{
    //function that gets current checksums for all db tables on the server,compares them to the local database
    //and then requests an SQL-string from the server to update the tables that needs it.
    //I don't think this is relevant for my problem,and it runs fine anyways.

    //The last thing it does is setting up a NSURLConnection to communicate with 
    //the server (sending which tables to request via POST and then getting the SQL-string as the server response)

        NSURLConnection *theConnection=[[NSURLConnection alloc] initWithRequest:request delegate:self];
        if (theConnection) {
            //receivedData = [NSMutableData data];
            NSLog(@"ReceivedData är: %d",[receivedData length]);
        } else {
            NSLog(@"Connection Failed!");
        }

        //All done here,now the ReceivedData method takes over

    }
}

更新:

这是我的viewDidLoad方法:

- (void)viewDidLoad
{
    //creates database and fills "tempArray" with data

        NSLog(@"%d",sqlite3_finalize(statement));
        sqlite3_close(contactDB);

        myTableViewDataSource = tempArray;
        NSLog(@"myTableViewDataSource count: %d",[myTableViewDataSource objectAtIndex:i]);
        }

    }

    myTableView = [[UITableView alloc] initWithFrame:self.view.frame style:UITableViewStylePlain];
    myTableView.delegate = self;   //Added this after comment (1)
    myTableView.dataSource = self; //Added this after comment (2)
}

更新:
添加后:

myTableView.delegate = self;
myTableView.dataSource = self;

to viewDidLoad,tableView:numberOfRowsInSection:被调用,而dataSource不是nil,但是没有调用tableView:cellForRowAtIndexPath:,并且表没有被更新.

更新2:
这是我的头文件:

//
//  FirstViewController.h
//  OHBSYS Storyboards
//
//  Created by David Forsberg on 2012-09-25.
//  Copyright (c) 2012 David Forsberg. All rights reserved.
//

#import <UIKit/UIKit.h>
#import <sqlite3.h>

@interface FirstViewController : UIViewController <UITableViewDataSource,UITableViewDelegate>{
    NSString *databasePath;
    sqlite3 *contactDB;

    UITextView *myTextView;
    UITableView *myTableView;

    NSMutableArray *myTableViewDataSource;

    NSMutableString * tableRowCount;
    NSMutableArray * dbloop1;

    //And a bunch of other variables here
}

@property (retain,nonatomic) IBOutlet UITextView *myTextView;
@property (nonatomic,retain) IBOutlet UITableView *myTableView;
@property (nonatomic,retain) IBOutlet NSMutableArray *myTableViewDataSource;

- (IBAction) fetchData;

@property (nonatomic) NSString * tableRowCount;
@property (nonatomic,retain) NSMutableArray * dbloop1;

//A bunch of other properties here as well

@end

更新3:

我已经尝试使用断点检查值.

tableview内的断点:numberofrowsinsection:

(lldb) po self.myTableView
(UITableView *) $0 = 0x0d13ca00 <UITableView: 0xd13ca00; frame = (0 20; 768 1004); clipsToBounds = YES; layer = <CALayer: 0x2a3d30>; contentOffset: {0,0}>
(lldb) po self.myTableView.delegate
(objc_object *) $1 = 0x0ce99500 <FirstViewController: 0xce99500>
(lldb) po self.myTableView.dataSource
(objc_object *) $2 = 0x0ce99500 <FirstViewController: 0xce99500>
(lldb) po self.myTableViewDataSource
(NSMutableArray *) $3 = 0x0029fb20 <__NSArrayM 0x29fb20>(
CONTACTS,sqlite_sequence
)

(lldb)

数据库更新后的断点,就在调用reloadData之前:

(lldb) po self.myTableView
(UITableView *) $4 = 0x0d13ca00 <UITableView: 0xd13ca00; frame = (0 20; 768 1004); clipsToBounds = YES; layer = <CALayer: 0x2a3d30>; contentOffset: {0,0}>
(lldb) po self.myTableView.delegate
(objc_object *) $5 = 0x0ce99500 <FirstViewController: 0xce99500>
(lldb) po self.myTableView.dataSource
(objc_object *) $6 = 0x0ce99500 <FirstViewController: 0xce99500>
(lldb) po self.myTableView.dataSource //(accidentally hit that twice)
(objc_object *) $7 = 0x0ce99500 <FirstViewController: 0xce99500>
(lldb) po self.myTableViewDataSource
(NSMutableArray *) $8 = 0x0ce6baa0 <__NSArrayM 0xce6baa0>(
CONTACTS,meta_tablechecksums,prot_multicoltest,prot_multicoltest_4,prot_multicoltest_4_desc,prot_multicoltest_desc,sqlite_sequence,superadmin_filefolders,superadmin_files,superadmin_imagefolders,superadmin_images,sys_customers,sys_fieldlooks,sys_fieldtypes,sys_formtables,sys_pages,sys_subpages,sys_userroles,sys_users
)

(lldb)

断点AT reloadData行:

(lldb) po self.myTableView
(UITableView *) $9 = 0x0d13ca00 <UITableView: 0xd13ca00; frame = (0 20; 768 1004); clipsToBounds = YES; layer = <CALayer: 0x2a3d30>; contentOffset: {0,0}>
(lldb) po self.myTableView.delegate
(objc_object *) $10 = 0x0ce99500 <FirstViewController: 0xce99500>
(lldb) po self.myTableView.dataSource
(objc_object *) $11 = 0x0ce99500 <FirstViewController: 0xce99500>
(lldb) po self.myTableViewDataSource
(NSMutableArray *) $12 = 0x0ce6baa0 <__NSArrayM 0xce6baa0>(
CONTACTS,sys_users
)

(lldb)

tableview中的断点:numberofrowsinsection:在更新db并运行reloadData命令之后:

(lldb) po self.myTableView
(UITableView *) $13 = 0x0d13ca00 <UITableView: 0xd13ca00; frame = (0 20; 768 1004); clipsToBounds = YES; layer = <CALayer: 0x2a3d30>; contentOffset: {0,0}>
(lldb) po self.myTableView.delegate
(objc_object *) $14 = 0x0ce99500 <FirstViewController: 0xce99500>
(lldb) po self.myTableView.dataSource
(objc_object *) $15 = 0x0ce99500 <FirstViewController: 0xce99500>
(lldb) po self.myTableViewDataSource
(NSMutableArray *) $16 = 0x0ce6baa0 <__NSArrayM 0xce6baa0>(
CONTACTS,sys_users
)

(lldb)

解决方法

我没有看到myTableView被合成.
在执行reloadData时,还调用了tableView数据源方法吗?

如果没有,请仔细检查并验证myTableView委托是否设置正确.
如果您已正确地将表连接到笔尖并且您有一个插座,您也可以在代码中设置它,即在viewDidLoad方法中,通过设置:

myTableView.delegate = self;

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐


当我们远离最新的 iOS 16 更新版本时,我们听到了困扰 Apple 最新软件的错误和性能问题。
欧版/美版 特别说一下,美版选错了 可能会永久丧失4G,不过只有5%的概率会遇到选择运营商界面且部分必须连接到iTunes才可以激活
一般在接外包的时候, 通常第三方需要安装你的app进行测试(这时候你的app肯定是还没传到app store之前)。
前言为了让更多的人永远记住12月13日,各大厂都在这一天将应用变灰了。那么接下来我们看一下Flutter是如何实现的。Flutter中实现整个App变为灰色在Flutter中实现整个App变为灰色是非常简单的,只需要在最外层的控件上包裹ColorFiltered,用法如下:ColorFiltered(颜色过滤器)看名字就知道是增加颜色滤镜效果的,ColorFiltered( colorFilter:ColorFilter.mode(Colors.grey, BlendMode.
flutter升级/版本切换
(1)在C++11标准时,open函数的文件路径可以传char指针也可以传string指针,而在C++98标准,open函数的文件路径只能传char指针;(2)open函数的第二个参数是打开文件的模式,从函数定义可以看出,如果调用open函数时省略mode模式参数,则默认按照可读可写(ios_base:in | ios_base::out)的方式打开;(3)打开文件时的mode的模式是从内存的角度来定义的,比如:in表示可读,就是从文件读数据往内存读写;out表示可写,就是把内存数据写到文件中;
文章目录方法一:分别将图片和文字置灰UIImage转成灰度图UIColor转成灰度颜色方法二:给App整体添加灰色滤镜参考App页面置灰,本质是将彩色图像转换为灰度图像,本文提供两种方法实现,一种是App整体置灰,一种是单个页面置灰,可结合具体的业务场景使用。方法一:分别将图片和文字置灰一般情况下,App页面的颜色深度是24bit,也就是RGB各8bit;如果算上Alpha通道的话就是32bit,RGBA(或者ARGB)各8bit。灰度图像的颜色深度是8bit,这8bit表示的颜色不是彩色,而是256
领导让调研下黑(灰)白化实现方案,自己调研了两天,根据网上资料,做下记录只是学习过程中的记录,还是写作者牛逼
让学前端不再害怕英语单词(二),通过本文,可以对css,js和es6的单词进行了在逻辑上和联想上的记忆,让初学者更快的上手前端代码
用Python送你一颗跳动的爱心
在uni-app项目中实现人脸识别,既使用uni-app中的live-pusher开启摄像头,创建直播推流。通过快照截取和压缩图片,以base64格式发往后端。
商户APP调用微信提供的SDK调用微信支付模块,商户APP会跳转到微信中完成支付,支付完后跳回到商户APP内,最后展示支付结果。CSDN前端领域优质创作者,资深前端开发工程师,专注前端开发,在CSDN总结工作中遇到的问题或者问题解决方法以及对新技术的分享,欢迎咨询交流,共同学习。),验证通过打开选择支付方式弹窗页面,选择微信支付或者支付宝支付;4.可取消支付,放弃支付会返回会员页面,页面提示支付取消;2.判断支付方式,如果是1,则是微信支付方式。1.判断是否在微信内支付,需要在微信外支付。
Mac命令行修改ipa并重新签名打包
首先在 iOS 设备中打开开发者模式。位于:设置 - 隐私&安全 - 开发者模式(需重启)
一 现象导入MBProgressHUD显示信息时,出现如下异常现象Undefined symbols for architecture x86_64: "_OBJC_CLASS_$_MBProgressHUD", referenced from: objc-class-ref in ViewController.old: symbol(s) not found for architecture x86_64clang: error: linker command failed wit
Profiles >> 加号添加 >> Distribution >> "App Store" >> 选择 2.1 创建的App ID >> 选择绑定 2.3 的发布证书(.cer)>> 输入描述文件名称 >> Generate 生成描述文件 >> Download。Certificates >> 加号添加 >> "App Store and Ad Hoc" >> “Choose File...” >> 选择上一步生成的证书请求文件 >> Continue >> Download。
今天有需求,要实现的功能大致如下:在安卓和ios端实现分享功能可以分享链接,图片,文字,视频,文件,等欢迎大佬多多来给萌新指正,欢迎大家来共同探讨。如果各位看官觉得文章有点点帮助,跪求各位给点个“一键三连”,谢啦~声明:本博文章若非特殊注明皆为原创原文链接。