我有 2 个 View -view1 和 view2,一个用于输入数据,另一个用于显示输入的数据。 我能够在标签的 view2 中显示输入的数据。但是如何在 UITableView 中显示数据。以下是我显示数据的代码:
view2.m
@synthesize details; //details is an object of NSMutableArray.
- (void)viewWillAppear:(BOOL)animated {
[super viewWillAppear:animated];
textLabel.text = self.name;
lblCity.text = self.city;
lblGender.text = self.gender;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return [details count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *simpleTableIdentifier = @"SimpleTableItem";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:simpleTableIdentifier];
}
cell.textLabel.text = [details objectAtIndex:indexPath.row];
//cell.imageView.image = [UIImage imageNamed:@"creme_brelee.jpg"];
return cell;
我调试并发现 cellForRowAtIndexPath
从未被调用。
我哪里错了?我该如何解决?
最佳答案
您需要在 UITableViewCell
中创建一个显示数据的 NSMutableArray像下面一样,不要忘记在 .h 文件中声明 UITableView 委托(delegate) <UITableViewDataSource,UITableViewDelegate>
并在您的 xib 中连接您的 UITableView IBOutlet 并连接委托(delegate):
- (void)viewDidLoad
{
[super viewDidLoad];
arrCategorisation=[[NSMutableArray alloc] initWithObjects:@"DataOne",@"DataTwo",@"DataThree", @"DataFour",@"DataFive",nil];
}
#pragma mark - Table view data source
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
// Return the number of sections.
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return arrCategorisation.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
NSString *CellIdentifier =[NSString stringWithFormat:@"%d",indexPath.row];
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
cell.textLabel.text=[arrCategorisation objectAtIndex:indexPath.row];
}
// Configure the cell...
return cell;
}
#pragma mark - Table view delegate
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
}
您的表加载数据如下:-
关于iphone - 在 View 导航时在 UITableView 中显示数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14232720/