【问题标题】:display data in UITableView on view navigation在视图导航的 UITableView 中显示数据
【发布时间】:2012-12-23 08:28:00
【问题描述】:

我有 2 个视图 - 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 从未被调用过。

我哪里错了?如何解决?

【问题讨论】:

  • 您是否将tableView 的dataSourcedelegate 设置为您的对象?
  • 对不起,我是 ios 新手,如何设置它们?
  • 如果您在 xib 中使用了表,则将其委托方法设置为文件所有者。看到这个techotopia.com/index.php/…
  • 转到您的xib文件并选择您的表格并右键单击表格并将委托和数据源附加到文件所有者
  • @z22 进入Interface Builder,右键点击你的UITableView,你会找到两个出口,delegatedataSource。单击小圆圈并将蓝线拖到您的文件所有者/您的对象。

标签: iphone ios uitableview uinavigationcontroller


【解决方案1】:

您需要在UITableViewCell 中创建一个显示数据的 NSMutableArray,如下所示,不要忘记在 .h 文件 <UITableViewDataSource,UITableViewDelegate> 中声明 UITableView 委托,并将您的 UITableView IBOutlet 连接到您的xib 并连接代理:

- (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
{

}

您的表加载数据如下:-

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多