Ios 在不带情节提要的objective-c代码上创建表
我无法在objective-c上创建表格,因为我开始学习objective-c 谷歌帮不了我,我试过了。 我希望你能帮助我 //更新:下面尼克给出了正确的答案。过了一段时间,我被提示回答这个问题,现在我可以与大家分享答案。我想对像我这样的人有用Ios 在不带情节提要的objective-c代码上创建表,ios,objective-c,xcode,Ios,Objective C,Xcode,我无法在objective-c上创建表格,因为我开始学习objective-c 谷歌帮不了我,我试过了。 我希望你能帮助我 //更新:下面尼克给出了正确的答案。过了一段时间,我被提示回答这个问题,现在我可以与大家分享答案。我想对像我这样的人有用 - (void)viewDidLoad { [super viewDidLoad]; //Here we initialize the table, create delegates and display it on the scree
- (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;
}
对于带有数组的默认tableview单元格:
- (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;
}
如果您正在使用XIB
您必须创建tablviewcell文件并设计自己的单元格
也导入您的单元格文件
#导入“selectedcell.h”
看这个例子:勾选:@Nick我很抱歉,我不知道如何投票支持你的评论,我可以投票支持你的答案。若你们写下答案,我会投赞成票:)答案是正确的,请帮助我。非常感谢。它的解决方案对我很有用。非常感谢。
- (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;
}