iphone - 在 View 导航时在 UITableView 中显示数据

标签 iphone ios uitableview uinavigationcontroller

我有 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
{

}

您的表加载数据如下:-

enter image description here

关于iphone - 在 View 导航时在 UITableView 中显示数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14232720/

相关文章:

Objective-C ARC 和传递 C 对象数组

ios - 从 firebase 存储中检索图像以显示在 tableView 单元格上

iphone - UITextView 中的两列文本?

iOS:以编程方式从应用程序退出到主屏幕并优雅地退出动画?

ios - 像在 Twitter 应用程序中那样显示相机胶卷?

ios - Firebase 通知在手机之间不起作用

ios - GMSMarker 不透明度动画不重复

ios - 为什么我的阴影层位置不正确?

ios - 环球银行金融电信协会3 : How to add headers to columns in UITableViewController?

ios - 无法将类型 'UITableViewCell' 的值转换为指定类型