UITableViewCell with xib [英] UITableViewCell with xib
问题描述
我正在开发一个使用xib文件的iOS应用程序。
I'm developing an app for iOS which use the xib files.
通常我使用Storyboard和我不知道如何使用xib文件设置UITableViewCell
。当我使用UITableView创建一个xib文件时,我看到一个有一些行的表,现在我需要编辑这一行来写入我存储在数组中的行。
Usually I use Storyboard and I don't know how to set up a UITableViewCell with xib files
. When I make a xib file with a UITableView, I see a table with some row, now I need to edit this row to write what I stored in an array.
strong>如何使用xib文件来设计UITableViewCell?
How can I use the xib file to design a UITableViewCell?
我需要做一个非常简单的表:我将使用基本预设单元格显示标题。
我知道我必须将委托和DataSource连接到表视图的文件所有者,我把文件所有者的UITableViewDelegate和UITableViewDataSource。
I need to do a very simple table: I would to use the Basic preset for the cell to display a title. I know that I've to connect the delegate and DataSource to the file owner for table view and I put in the file owner the UITableViewDelegate and UITableViewDataSource.
现在如何编辑单元格的内容?
我在网上找到一些指导,告诉我使用UITableViewCell创建一个xib文件,我做到了,但我不知道如何使用此
Now how I can edit the content of the cell? I found on the web some guide that tells to create a xib file with a UITableViewCell and I did it, but I don't know how to work with this
推荐答案
首先,需要为继承自UITableViewCell的customCell创建类。
现在,添加您想要在您的customCell中的属性。在这个例子中,我添加了cellImage和cellLabel。
Firstly, you need to create the class for the customCell which inherits from UITableViewCell. Now, add the properties you want to have in your customCell. In this example I added cellImage and cellLabel.
@property (nonatomic, strong) IBOutlet UILabel *cellLabel;
@property (nonatomic, strong) IBOutlet UIImageView *cellImageView;
之后,您需要将UILabel和UIImageView从CustomCell链接到Nib。
After that you need to link the UILabel and the UIImageView from the CustomCell to the Nib.
您需要添加:
- (void)viewDidLoad
{
....
[self.tableView registerNib:[UINib nibWithNibName:@"xibName" bundle:nil] forCellReuseIdentifier:CellIdentifier];
.....
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"CustomCellReuse";
CustomCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[CustomCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
// Configure the cell...
[cell.cellImageView setImage:[UIImage imageNamed:@"whatever"]];
[cell.cellLabel setText = @"whatever"];
return cell;
}
这篇关于UITableViewCell with xib的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!