TableView的cellForRowAtIndexPath不执行,不过NumberofRow这个函数返回的值是正确的,包括NumberofSections的值也没问题。
我用的是Storyboard, 自己定义的TableView,
能执行到 NumberofRow, 就是不执行cellForRow!
在xcode4中 xib下是没问题的
我在xcode5中用了Storyboard, 代码是一样的, 包括定义的也都是一样的, 结果不显示! 是不是有什么绑定我没做到? ViewController.h:@interface ViewController : UIViewController
<UITableViewDataSource, UITableViewDelegate>
@property (copy, nonatomic) NSMutableArray *prjlist;
@property (nonatomic, strong) UITableView *mytable;
.m
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{ return [self.prjlist count];
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
NSLog(@"%i", self.prjlist.count); //能执行到,值正确
return 1;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
//不执行
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = (UITableViewCell *) [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier];
} NSMutableDictionary *prjinfo = self.prjlist[indexPath.row]; cell.textLabel.text = [prjinfo objectForKey:@"prj_name"];
cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
return cell;}
我用的是Storyboard, 自己定义的TableView,
能执行到 NumberofRow, 就是不执行cellForRow!
在xcode4中 xib下是没问题的
我在xcode5中用了Storyboard, 代码是一样的, 包括定义的也都是一样的, 结果不显示! 是不是有什么绑定我没做到? ViewController.h:@interface ViewController : UIViewController
<UITableViewDataSource, UITableViewDelegate>
@property (copy, nonatomic) NSMutableArray *prjlist;
@property (nonatomic, strong) UITableView *mytable;
.m
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{ return [self.prjlist count];
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
NSLog(@"%i", self.prjlist.count); //能执行到,值正确
return 1;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
//不执行
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = (UITableViewCell *) [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier];
} NSMutableDictionary *prjinfo = self.prjlist[indexPath.row]; cell.textLabel.text = [prjinfo objectForKey:@"prj_name"];
cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
return cell;}
ViewController.h:@interface ViewController : UIViewController
<UITableViewDataSource, UITableViewDelegate>
要拖一下的。
要不他哪知道是哪个tableview要用啊。
UITableView *mytable我在storyboard里拖到哪里呢?
{
self. mytable = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height)];
self. mytable.separatorStyle = UITableViewCellSeparatorStyleNone;
self. mytable.delegate = self;
self. mytable.dataSource = self;
...
}
因为是异步的, 所以我用了 [self.mytable reloadData]在xcode4下, reload数据就出来了
在xcode5下面 就不显示了
self.mytable.dataSource = self;
2.UI处理只能在主线程, [self.mytable reloadData]需要在主线程执行
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
没有实现
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
粗心。
实现numberofRowsInSection就会调用cellForRowAtIndexPath了,当然前提是你storyBoard里把tableView控件的datasource和delegate连上你的viewcontroler
swift里面numberofRowsInSection这个方法是可选的啊,不实现也可以运行cellForRowAtIndexPath代理