Objective c 目标C:如何通过代码创建自定义/静态表视图单元格
我正在尝试使用代码(没有nib)创建3个表视图单元格。我在使代码正常工作时遇到了一些问题。我想我的方法不对。谁能告诉我正确的前进方向吗?在此方面的任何帮助都将不胜感激 谢谢 郑和 我的代码片段如下所示:Objective c 目标C:如何通过代码创建自定义/静态表视图单元格,objective-c,ios,uitableview,Objective C,Ios,Uitableview,我正在尝试使用代码(没有nib)创建3个表视图单元格。我在使代码正常工作时遇到了一些问题。我想我的方法不对。谁能告诉我正确的前进方向吗?在此方面的任何帮助都将不胜感激 谢谢 郑和 我的代码片段如下所示: - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView { // Return the number of sections. return 1; } - (NSInteger)tableView:(
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
// Return the number of sections.
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
// Return the number of rows in the section.
return 3;
}
// Customize the appearance of table view cells.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"Cell";
int row = [indexPath row];
UITableViewCell *startCell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier] autorelease];
UITableViewCell *durationCell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier] autorelease];
UITableViewCell *radiusCell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier] autorelease];
startCell.textLabel.text = @"Start:";
durationCell.textLabel.text = @"Duration:";
radiusCell.textLabel.text = @"radius";
if (row == 0)
{
return startCell;
}
else if (row == 1)
{
return durationCell;
}
return radiusCell;
}
编辑(19/05/11)
看完您的答案后,我仍然无法在tableview中显示任何单元格。这是因为我初始化表的方式吗
//Initialization
UITableView *tv = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.settingsView.frame.size.height)
style:UITableViewStyleGrouped];
self.tableView = tv;
[self.view addSubview:tableView];
之后,我有一个动画来展开tableView
[UIView animateWithDuration:0.3 animations:^{
[self.tableView setFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height-self.picker.frame.size.height)];
}];
你们觉得上面有什么问题吗?有什么导致我的单元格显示失败的吗
谢谢
Zhen一次不要创建多个单元格,并通过
dequeueReusableCellWithIdentifier:
使用重用机制。您可以根据单元格的索引路径配置单元格(从模型馈送数据等)
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
}
// configure cell...
switch(indexPath.row) { // assuming there is only one section
case 0:
cell.textLabel.text = @"Start:";
break;
case 1:
cell.textLabel.text = @"Duration:";
break;
case 2:
cell.textLabel.text = @"radius";
break;
default:
break;
}
return cell;
}
tableView:cellforrowatinexpath:
方法在表视图中每行调用一次。您不需要每次创建3个单元格。为此方法的每次调用创建一个单元格,并返回它。类似这样的事情-
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"Cell";
int row = [indexPath row];
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:nil];
if (cell == nil) {
UITableViewCell *cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier] autorelease];
}
switch (row) {
case 1:{
cell.textLabel.text = @"Start";
break;
}
case 2:{
cell.textLabel.text = @"Duration";
break;
}
default:
cell.textLabel.text = @"radius";
break;
}
return cell;
}
在您的
UITableView
初始化代码中,我看不到您在哪里设置了表视图delegate
和dataSource
。这当然是问题的一部分。根据苹果公司的:
UITableView对象必须具有用作数据源的对象和用作委托的对象;通常,这些对象是应用程序委托,或者更常见的是自定义UITableViewController对象。数据源必须采用UITableViewDataSource协议,代理必须采用UITableViewDelegate协议。数据源提供UITableView在插入、删除或重新排序表行时构造表和管理数据模型所需的信息。委托提供表格使用的单元格并执行其他任务,如管理附件视图和选择
这是您可以尝试做的:
//Initialization
UITableView *tv = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.settingsView.frame.size.height)
style:UITableViewStyleGrouped];
// assuming that your controller adopts the UITableViewDelegate and
// UITableViewDataSource protocols, add the following 2 lines:
tv.delegate = self;
tv.dataSource = self;
self.tableView = tv;
[self.view addSubview:tableView];
您应该执行
self.view=self.tableView代码>。在UITableViewController
中,它们是相同的。您是否设置了数据源和委托?虽然方法不完全正确,但应该可以。你能告诉我它是怎么失败的吗?嗨,我的tableview上没有任何显示。我已经包括了我的初始化代码。你看到什么问题了吗?谢谢你设置好了吗?嗨,我按照你的代码做了,但还是无法显示。我已经更新了我的问题,以包括我初始化和设置表格视图动画的方式。你看到有什么问题吗?你试过其他人(@octy和@Deepak)的建议吗?您可能没有设置委托/数据源。您是否在代码中设置了断点以查看是否命中?您好,谢谢您的回复。我已经编辑了我的问题,以包括我初始化表视图的方式。不确定是否存在任何问题。如果你能给我这个建议,那就太好了。谢谢