首页 > 解决方案 > 在没有情节提要的 Objective-c 代码上创建一个表

问题描述

我无法在objective-c 上创建表格,因为我开始学习objective-c。

谷歌帮不了我,我试过了。我希望你能帮助我

//更新:下面尼克显示了正确的答案。过了一段时间,我被提示回答这个问题,现在我可以和你分享答案。我想对像我这样的人有用

- (void)viewDidLoad
{
    [super viewDidLoad];
    //Here we initialize the table, create delegates and display it on the screen
    arrData=[[NSArray alloc]initWithObjects:@"ONE",@"TWO",,@"THREE", nil];
    tableView = [[UITableView alloc] initWithFrame:self.view.bounds style:UITableViewStylePlain];
    tableView.delegate = self;
    tableView.dataSource = self;

    [self.view addSubview:tableView];
}

#pragma mark - UITableView DataSource & Delegate
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:    (NSInteger)section
{
    return arrData.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView  cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    //Cells initialization
    static NSString *identifier = @"cell";

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
    if (cell == nil)
    {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier];
    }
    cell.textLabel.text = [arrData objectAtIndex:indexPath.row];
    return cell;
}

标签: iosobjective-cxcode

解决方案


对于带有数组的默认表格视图单元格:

- (void)viewDidLoad
{
    [super viewDidLoad];

    arrData=[[NSArray alloc]initWithObjects:@"ONE",@"TWO",,@"THREE", nil];
    tableView = [[UITableView alloc] initWithFrame:self.view.bounds style:UITableViewStylePlain];
    tableView.delegate = self;
    tableView.dataSource = self;

    [self.view addSubview:tableView];
}

#pragma mark - UITableView DataSource & Delegate
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:    (NSInteger)section
{
    return arrData.count;
}

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

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
    if (cell == nil)
    {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier];
    }
    cell.textLabel.text = [arrData objectAtIndex:indexPath.row];
    return cell;
}

推荐阅读