ios - 在没有情节提要的 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;
}
解决方案
对于带有数组的默认表格视图单元格:
- (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;
}
推荐阅读
- python - 如何实现深度卷积神经网络的反向传播?
- python - 如何在 Python 中使用 BAC0 readRange
- python-3.x - 如何从 RSA 公钥计算指纹?
- python - 如何创建多个包含我所有地块的地块
- reactjs - 如何将对象传递到 ant.design 表单中?
- python - 当随机数生成器在列表中移动时,如何改变它的概率?
- kubernetes - 发布管道 - Helm 升级失败
- javascript - 我想创建一个 mat-checkbox 更改事件,它可以显示或隐藏卡片
- android - Android Studio - 使用意图参数时应用程序崩溃
- python - 如何在 Windows 10 64 位上通过 Python Idle 3.8.6 安装 nltk 语料库?