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/

相关文章:

iphone - 是否有任何第三方工具可以为 armv6/armv7 创建静态库?

iOS 通用应用程序 - UITableView/UICollectionView

ios - 导航 Controller 的问题(后退按钮和prepareForSegue)

iphone - 通过单击按钮替换滑动以删除的操作

iOS 模拟器恢复位置设置

iphone - 分配错误NSString?

ios - dispatch_source_t 处理函数的计时问题——我是否遵循正确的调度计时器模式?

ios - 无法在 viewDidLoad 方法中获取传递的数据

ios - 根据语言环境查找小数点字符

ios - 可拖动的 UIButton 不响应 UIControlEvents