【问题标题】:Create a table on objective-c code without storyboard在没有情节提要的 Objective-c 代码上创建一个表
【发布时间】:2020-02-12 23:12:35
【问题描述】:

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

Google 帮不了我,我试过了。 希望你能帮到我

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

- (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;
}

【问题讨论】:

标签: ios objective-c xcode


【解决方案1】:

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

- (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;
}

【讨论】:

  • 它的解决方案是正确的并帮助了我。谢谢!
【解决方案2】:

如果您使用的是 XIB

您必须创建 tablviewcell 文件并设计自己的单元格。

也导入您的单元格文件..

#import "selectedcell.h"

- (void)viewDidLoad {

[super viewDidLoad];
array =[[NSArray alloc]initWithObjects:@"AAAA",@"BBBB",,@"CCCC", nil];
tableView = [[UITableView alloc] initWithFrame:self.view.bounds 
style:UITableViewStylePlain];
tableView.delegate = self;
tableView.dataSource = self;
[self.tablview registerNib:[UINib nibWithNibName:@"SelectedTableViewCell" 
bundle:nil] forCellReuseIdentifier:@"cellidentifier"];
self.tablview.tableFooterView = [[UIView alloc]initWithFrame:CGRectZero];
[self.view addSubview:tableView];
}

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

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{

    SelectedTableViewCell *cell = [tableView 
    dequeueReusableCellWithIdentifier:@"cellidentifier" forIndexPath:indexPath];

    cell.selectionStyle = UITableViewCellSelectionStyleGray;
    cell.label.text = [Nsstring stringwithformate:@"%@",[array objectatindex:indexpath.row]];

    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
   //After selecting row
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 50;
}

【讨论】:

  • 它的解决方案对我很有用。谢谢!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2015-07-17
  • 2013-12-09
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多