Ios UITableView不显示数据。
我是iOS新手,只是想让数据显示在Ios UITableView不显示数据。,ios,uitableview,Ios,Uitableview,我是iOS新手,只是想让数据显示在UITableView中。我基本上有一个关于作者的项目。我想显示作者姓名等。因此我有一个作者模型和AuthorsViewController,这是UITableView等的数据源和委托。我正在使用故事板(MainStoryboard)表视图,并设法将其连接到“身份检查器”中的AuthorsViewController 情节提要图像如果有帮助,谢谢: 这里,首先是模型:Author.h #import <Foundation/Foundation.h>
UITableView
中。我基本上有一个关于作者的项目。我想显示作者姓名等。因此我有一个作者模型和AuthorsViewController
,这是UITableView
等的数据源和委托。我正在使用故事板(MainStoryboard
)表视图,并设法将其连接到“身份检查器”中的AuthorsViewController
情节提要图像如果有帮助,谢谢:
这里,首先是模型:Author.h
#import <Foundation/Foundation.h>
@interface Author : NSObject
@property (nonatomic, strong) NSString *name;
@property (nonatomic, strong) NSString *book;
@property (nonatomic) int year;
@end
这是AuthorsViewController.h
@interface AuthorViewController : UITableViewController
<UITableViewDataSource, UITableViewDelegate>
@end
#pragma标记-表视图数据源
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return [_authors count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"AuthorCell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
if (cell != nil)
{
cell = [[UITableViewCell alloc]
initWithStyle: UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
Author *currentAuthor = [_authors objectAtIndex:[indexPath row]];
[[cell textLabel] setText: [currentAuthor name]];
NSLog(@"%@", [currentAuthor name]);
return cell;
}
@end
由于在序列图像板中创建了表视图及其单元格,因此根本不需要if(cell==nil)子句。您还需要调用[self.tableView reloadData]作为viewDidLoad中的最后一行 不确定您的具体问题是什么,但是
CellForRowatineXpath
中的if(cell!=nil)
行应该是if(cell==nil)
。是否调用CellForRowatineXpath?你能把断点放在方法里面,看看它是否进入,如果不是,我怀疑,数据源设置有问题。我认为应该是如果(cell==nil),也可能是他为同一个变量分配了3次内存吗??可能有3个独立的变量名?确保UITableView将其委托和数据源设置为视图控制器。在其中一个委托方法中设置断点,以确保此部分已正确连接。是的!它应该是“if(cell==nil)”而不是“if(cell!=nil)”更改了该行,并且它工作了。顺便提一下,即使在“viewDidLoad”中没有“[self.tableView reloadData]”它也可以工作,这正常吗?谢谢你。@TendaiK,这取决于数据的加载方式。我猜在您的例子中,数据是在表调用其数据源方法之前设置的。是否删除了if(cell==nil)子句?在序列图像板中创建表视图时不需要它。
#import "AuthorViewController.h"
@interface AuthorViewController ()
@property (nonatomic, strong) NSMutableArray *authors;
@end
@implementation AuthorViewController
- (id)initWithStyle:(UITableViewStyle)style
{
self = [super initWithStyle:style];
if (self) {
// Custom initialization
}
return self;
}
- (void)viewDidLoad
{
[super viewDidLoad];
_authors = [[NSMutableArray alloc] init];
Author *auth = [[Author alloc] init];
[auth setName:@"David Powers"];
[auth setBook:@"PHP Solutions"];
[auth setYear:2010];
[_authors addObject:auth];
auth = [[Author alloc] init];
[auth setName:@"Lisa Snyder"];
[auth setBook:@"PHP security"];
[auth setYear:2011];
[_authors addObject:auth];
auth = [[Author alloc] init];
[auth setName:@"Rachel Andrew"];
[auth setBook:@"CSS3 Tips, Tricks and Hacks"];
[auth setYear:2012];
[_authors addObject: auth];
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return [_authors count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"AuthorCell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
if (cell != nil)
{
cell = [[UITableViewCell alloc]
initWithStyle: UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
Author *currentAuthor = [_authors objectAtIndex:[indexPath row]];
[[cell textLabel] setText: [currentAuthor name]];
NSLog(@"%@", [currentAuthor name]);
return cell;
}
@end