1

我有一个以编程方式实现的 tableView,采用分组样式。

我得到的只是应该填充的灰色细条纹。所以它正在加载,但不是......什么......

还需要什么?如果没有,那我还应该去哪里看?

另外,如何使表格的背景颜色与单元格的白色相同?

- (void)loadView {

    [super loadView];

    UITableView *view = [[UITableView alloc] initWithFrame:[UIScreen mainScreen].applicationFrame style:UITableViewStyleGrouped];

    [view setAutoresizingMask:UIViewAutoresizingFlexibleHeight|UIViewAutoresizingFlexibleWidth];

    self.view = view;

    [view reloadData];

}

viewDidLoad 是必要的吗?

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view.
}

谢谢,

  • 莫克罗姆
4

3 回答 3

2

您必须为您的 tableView 提供数据。

对于声明者,您需要定义一个dataSource. 通常只使用您的 viewController 作为数据源。

// in the .h find something similar and add <UITableViewDataSource>
@interface ViewController : UIViewController <UITableViewDataSource>

然后当你制作 tableView 时。

view.datasource = self;

然后,您需要提供数据本身。实现这些方法:

#pragma mark - UITableView Datasource

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 3;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return 3;
}

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

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];

    if (!cell) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];
    }

    [cell.textLabel setText:@"A Cell"];

    return cell;
}

这些方法将创建 3 个部分,每个部分有 3 行。所有的细胞都会说一个细胞。这是所有 tableViews 的基础。只需自定义数据:)

于 2013-01-30T15:47:51.953 回答
1

您需要为表视图设置 dataSource 和委托属性,以便能够从中提取数据:

UITableView *view = ...
view.dataSource = self;
view.delegate = self;
于 2013-01-30T15:45:41.723 回答
0

在 .h 文件中有协议,并将委托和源附加到文件所有者

于 2013-06-07T06:40:18.490 回答