关于table view cell选择控制器后出错

iOS 码拜 8年前 (2016-03-26) 1115次浏览
没有用一句代码,在弄了个table view cell运行后没问题,选择控制器后,即使控制器里没有代码,一运行,cell在模拟器上就不显示了,这是怎么回事。
解决方案

40

引用:
Quote: 引用:
Quote: 引用:
Quote: 引用:
Quote: 引用:
Quote: 引用:
Quote: 引用:
Quote: 引用:
Quote: 引用:
Quote: 引用:
Quote: 引用:
Quote: 引用:

代理设置了没有?

设置了啊。这个本人第一个就弄了

假如你是在storyboard里面设置代理的话,还是认真检查一下吧,有时会丢掉的,本人遇到过这种问题,最好还是在代码里面设置

self.tableview.delegate = self      
self.tableview.datasource= self
#pragma mark - Table view data source
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
#warning Incomplete implementation, return the number of sections
    return 0;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
#warning Incomplete implementation, return the number of rows
    return 0;
}

xcode7.3自动创建的这几行代码影响了视图的显示,删了就好了,不知道是为什么

什么叫不知道为什么,它默认的实现是section和numberofrow的值都是0啊,所以就什么都不显示了啊。

上一个问题自动作废。本人现在又遇到了选择控制器后就不显示表的问题了代码如下

#import "WCContactsViewController.h"
@interface WCContactsViewController ()
@property (nonatomic, strong) NSArray *friends;
@end
@implementation WCContactsViewController
- (void)viewDidLoad {
    [super viewDidLoad];
    
    // 从数据里加载好友列表显示
    [self loadFriends];
}
-(void)loadFriends{
    //使用CoreData获取数据
    // 1.上下文【关联到数据库XMPPRoster.sqlite】
    NSManagedObjectContext *context = [WCXMPPTool sharedWCXMPPTool].rosterStorage.mainThreadManagedObjectContext;
    
    
    // 2.FetchRequest【查哪张表】
    NSFetchRequest *request = [NSFetchRequest fetchRequestWithEntityName:@"XMPPUserCoreDataStorageObject"];
    
    // 3.设置过滤和排序
    // 过滤当前登录用户的好友
    NSString *jid = [WCUserInfo sharedWCUserInfo].jid;
    NSPredicate *pre = [NSPredicate predicateWithFormat:@"streamBareJidStr = %@",jid];
    request.predicate = pre;
    
    //排序
    NSSortDescriptor *sort = [NSSortDescriptor sortDescriptorWithKey:@"displayName" ascending:YES];
    request.sortDescriptors = @[sort];
    
    // 4.执行请求获取数据
    self.friends = [context executeFetchRequest:request error:nil];
    NSLog(@"%@",self.friends);
    
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.friends.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *ID = @"ContactCell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:ID];
    
    // 获取对应好友
    XMPPUserCoreDataStorageObject *friend =self.friends[indexPath.row];
    
    cell.textLabel.text = friend.jidStr;
    
    return cell;
}
@end

假如你确定设置了代理了的话,把下面这段加上

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
#warning Incomplete implementation, return the number of sections
    return 1;
}

还有要确保friends 这个数组是拥有值的

关于table view cell选择控制器后出错果然没有值啊,本人跟着视频做的,然后运行了人家的代码也没有值,本人解决不了这个问题了好急。

既然确认是数据源的问题,那么就好办了。
1、确认数据库存在
2、确认数据库拥有数据
3、确认从数据库获取数据成功
给篇关于CoreData框架入门的文章你看看:http://blog.csdn.net/q199109106q/article/details/8563438/

数据库存在,数据库里也确定有数据,但是就是不能获取= =
能不能把本人的整个代码发给你,帮本人看看。
http://www.codeios.com/forum.php?mod=viewthread&tid=204764&extra=这个帖子里本人上传了本人的代码。服务器用的openfire,本人设置的域名是wuziyi.local

没办法,试了很久都没办法连接上服务器,你用的是哪家的SDK?

你数据库应该是根据你的登录账户进行获取数据的吧?你要确保你这个账户的数据库有数据


CodeBye 版权所有丨如未注明 , 均为原创丨本网站采用BY-NC-SA协议进行授权 , 转载请注明关于table view cell选择控制器后出错
喜欢 (0)
[1034331897@qq.com]
分享 (0)