如何在 iPad 上的 TableView 中加载自定义 TableViewCell?
首先,对不起我的英语,我是法国人:)
就像标题中所说,我想将自定义 tableviewcell 放在 ipad 上的简单 tableview 中。这是必要的,因为我想生成一个有 5 个标签的表。
我使用带有 NIB 的单独 UITableViewCell
类。
这是我的代码:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"CellIdentifier";
CustomTableCell *cell = (CustomTableCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil)
{
[[NSBundle mainBundle] loadNibNamed:@"CustomTableCell" owner:self options:nil];
cell = customTableCell;
self.customTableCell = nil;
}
cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
[self configureCell:cell atIndexPath:indexPath];
NSString *rowLabelEcheance = [[NSString alloc] initWithFormat:@"%@", [contentsList objectAtIndex:indexPath.row]];
[customTableCell.labelEcheance setText:rowLabelEcheance];
NSString *rowLabelCapitalRestantDu = [[NSString alloc] initWithFormat:@"%@", >>>>>>> [contentsList2 objectAtIndex:indexPath.row]];
[customTableCell.labelCapitalRestantDu setText:rowLabelCapitalRestantDu];
NSString *rowLabelInterets = [[NSString alloc] initWithFormat:@"%@", [contentsList3 objectAtIndex:indexPath.row]];
[customTableCell.labelInterets setText:rowLabelInterets];
NSString *rowLabelAmortissement = [[NSString alloc] initWithFormat:@"%@", >[contentsList4 objectAtIndex:indexPath.row]];
[customTableCell.labelAmortissement setText:rowLabelAmortissement];
NSString *rowLabelMontant = [[NSString alloc] initWithFormat:@"%@", [contentsList5 objectAtIndex:indexPath.row]];
[customTableCell.labelMontant setText:rowLabelMontant];
return cell;
}
你能帮我吗?我花了 8 个小时来解决这个问题......
谢谢!
First, sorry for my english, I'm french :)
Like say in the title, I want to put a custom tableviewcell in a simple tableview on ipad. This is necessary because I want to generate a table with 5 labels.
I use a separate UITableViewCell
Class with a NIB.
This is my code :
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"CellIdentifier";
CustomTableCell *cell = (CustomTableCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil)
{
[[NSBundle mainBundle] loadNibNamed:@"CustomTableCell" owner:self options:nil];
cell = customTableCell;
self.customTableCell = nil;
}
cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
[self configureCell:cell atIndexPath:indexPath];
NSString *rowLabelEcheance = [[NSString alloc] initWithFormat:@"%@", [contentsList objectAtIndex:indexPath.row]];
[customTableCell.labelEcheance setText:rowLabelEcheance];
NSString *rowLabelCapitalRestantDu = [[NSString alloc] initWithFormat:@"%@", >>>>>>> [contentsList2 objectAtIndex:indexPath.row]];
[customTableCell.labelCapitalRestantDu setText:rowLabelCapitalRestantDu];
NSString *rowLabelInterets = [[NSString alloc] initWithFormat:@"%@", [contentsList3 objectAtIndex:indexPath.row]];
[customTableCell.labelInterets setText:rowLabelInterets];
NSString *rowLabelAmortissement = [[NSString alloc] initWithFormat:@"%@", >[contentsList4 objectAtIndex:indexPath.row]];
[customTableCell.labelAmortissement setText:rowLabelAmortissement];
NSString *rowLabelMontant = [[NSString alloc] initWithFormat:@"%@", [contentsList5 objectAtIndex:indexPath.row]];
[customTableCell.labelMontant setText:rowLabelMontant];
return cell;
}
Can you help me ? I spend 8 hours of work for this problem....
Thank you !
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
如果其他人遇到此问题,您可以在此处下载一个工作示例项目: https://github.com/ijoshsmith/CustomCellDemo
否则,这里是说明:
据我所知,有几种方法可以做到这一点。
假设以下名称和文件结构
使用 Interface Builder,要创建自定义表格单元格,您需要执行以下操作:
1.) 从继承自 UITableViewCell 的 NIB 创建自定义表格单元格(正如您所做的那样)
2.) 取消“文件的所有者”在IB中为您的自定义表格单元格类并将其指向您希望单元格出现在其中的表格类,例如将其更改为UITableViewCustomClass。
3.) 单击 IB 屏幕中的表格单元格,然后转到显示 UITableViewCell 的字段,并将其更改为从自定义表格单元格类继承,例如 UITableCellCustomClass
4.) 确保您已创建了所有字段和属性以及出口您的文本字段,并在界面生成器中在表格单元格类和自定义单元格 xib 中的单元格之间建立了必要的连接(注意:不是文件的所有者...)
5.)我看到您那里有“单元格标识符”变量...但以防万一您还没有这样做,请确保在 CustomTableCell xib 中您已更改单元格标识符字段以匹配您的自定义标识符。
如果我没记错的话,我相信应该可以了。看起来您正在正确处理表格并设置单元格的属性。
我相信这就是全部。我把它详细地写在某个地方,因为它太痛苦了。
更新:
如果您有五个标签,您可能需要更改表格行的垂直高度,以便显示所有标签(除非您有一个非常宽的表格)。
确保表的委托和数据源都指向它的父类(托管单元格加载逻辑的类)
您必须重写此函数:
更新:显然上面的方法还不够,所以我会再试一次:
尝试使用以下 UINib 方法:
将其添加到您的 UICustomTableView.h 类中
接下来,尝试以下操作(假设您遵循了上述所有步骤)...也许 UINib 加载程序会有所帮助?我很确定在查看我的笔记后我已经涵盖了所有正确的步骤。要尝试的一件事是创建一个扩展 UIViewController 的类,然后从中删除视图,添加表格单元格并执行上述步骤,并将基类更改为从 UITableCellView 继承。
If anyone else has this problem, you can download a working example project here: https://github.com/ijoshsmith/CustomCellDemo
Otherwise, here are the instructions:
There are a few ways of doing this that I know of.
Assuming the following names and file structure
With Interface Builder, to create a custom table cell you need to do this:
1.) Create a custom table cell from a NIB (as you have done) which inherits from UITableViewCell
2.) Unhook the "File's Owner" in IB for your custom table cell class and point it at the table class which you are wanting the cell to appear in e.g. change it to UITableViewCustomClass.
3.) Click the table cell in your IB screen and go to the field that says UITableViewCell and change it to inherit from your custom table cell class e.g. UITableCellCustomClass
4.) MAke sure you have created all the fields and properties as well as outlets for your textfields and made the necessary connections in interfacebuilder between your table cell class and the cell in your custom cell xib (note: NOT the file's owner...)
5.) I see you have the "cell identifier" variable there...but just in case you haven't yet, make sure that in your CustomTableCell xib you have changed the cell identifier field to match your custom identifier.
That should do it I believe if my memory serves correctly. It looks like you are doing the rest right with your table and setting the properties on the cell.
I believe that is all of it. I have it written down in explicit detail someplace because it is such a pain.
UPDATE:
IF you have five labels, you may need to alter the vertical height of the table rows so that all the labels will show (unless you have a really wide table).
Make sure the table has both it's delegate and data source pointed at it's parent class (the one hosting the cell loading logic)
You must override this function:
UPDATE: Apparently the above was not sufficient so I'll try again:
Try doing this using the following UINib Method:
Add this to your UICustomTableView.h class
Next, try the following (assuming you followed all the steps above)...maybe a UINib loader will help? I am pretty sure I covered all the proper steps after looking over my notes. One thing to try is to create a class that extends UIViewController, then delete the view from that, add your table cell and do teh steps above and change the base class to inherit from UITableCellView.
使用这段简单的代码:
Use this simple piece of code: