Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/93.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Iphone 创建UITableView的问题_Iphone_Ios_Objective C_Xcode - Fatal编程技术网

Iphone 创建UITableView的问题

Iphone 创建UITableView的问题,iphone,ios,objective-c,xcode,Iphone,Ios,Objective C,Xcode,您好,我是Xcode编程新手,在创建UITableView时有点困惑 我读了很多教程,并创建了TableViewController的子类 我已经有了一个UIViewController,其中有一个包含UITableView的xib文件 我已经在TableViewController子类中实现了必要的方法,但我不知道display如何在屏幕上显示TableView 我尝试将表委托设置为TableViewController,但该表未显示,并且它通过IBOutlet链接到我的UITableView

您好,我是Xcode编程新手,在创建
UITableView
时有点困惑

我读了很多教程,并创建了
TableViewController
的子类

我已经有了一个
UIViewController
,其中有一个包含
UITableView
的xib文件

我已经在
TableViewController
子类中实现了必要的方法,但我不知道display如何在屏幕上显示
TableView

我尝试将表委托设置为
TableViewController
,但该表未显示,并且它通过
IBOutlet
链接到我的
UITableView
上的属性

请有人告诉我我做错了什么

对不起,我英语不好

编辑: 非常感谢你的帮助

以下是我对该方法的代码:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    static NSString *CellIdentifier = @"Cell";

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue2 reuseIdentifier:CellIdentifier];
    }
    FichaItem * item = [sucursales objectAtIndex:indexPath.section];
    cell.textLabel.text = [item.campos objectAtIndex:indexPath.row];
    cell.detailTextLabel.text = [item.datos objectAtIndex:indexPath.row];
    return cell;
}

我真的看不出有什么不对,代码几乎被复制和修改以显示我的数据。

实现这一点有多种方法

方式1(我是这样做的):

打开viewController.h并继承UITableViewDelegate和UITableViewDataSource协议。看起来是这样的:
@interface ViewController:UIViewController

然后打开XIB,将UITableView对象拖动到视图中并将其连接起来:右键单击从tableView拖动到左侧文件的所有者对象。选择Delegate并为dataSource重做它

然后您只需要实现委托方法。您不需要为UITableViewController创建子类

方式2

子类UITableViewController。 将UITableView拖到视图中,并将“对象”添加到XIB中。然后将其类设置为tableViewController子类,并将其与tableView连接起来

可能还有其他几种方法可以做到这一点。但这些是我熟悉的方式

编辑: 下面是一个委托方法的示例实现

NSArray *dataSource = @[@{@"title": @"myTitle", @"subtitle": @"mySubTitle"}, @{@"title": @"some other title", @"subtitle": @"some explanation"}];

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"reusableCell"];

    if (!cell) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:@"reusableCell"];
    }

    // dataSource is an NSArray of NSDictionaries
    [[cell textLabel] setText:[[dataSource objectAtIndex:[indexPath row]] objectForKey:@"title"]];
    [[cell detailTextLabel] setText:[[dataSource objectAtIndex:[indexPath row]] objectForKey:@"subtitle"]];

    return cell;
}

如果您计划使用xib,请不要使用TableViewController子类,只使用普通的UIViewController。别忘了更改标识并从Xib连接委托和数据源。检查此链接的答案显示所有类型的表以及源代码和解释希望这有助于通过第一种方式显示表,但当我向下滚动然后备份数据时,数据消失,有人能解释为什么吗?这可能是由UITableViews可重用单元引起的。我会给你编一些代码