Dav*_*hta 4 objective-c nib ios
基本上我有一个自定义UITableViewCell,我在我的nib文件中.在其中,我有一些标签,以及我希望能够以编程方式编辑.我将我的nib文件称为"post",然后我将其文件所有者设为"post",并将其类设置为"post"在文件所有者下.我将笔尖中的所有标签链接到此类:
post.h:
#import <Foundation/Foundation.h>
@interface post : UITableViewCell
@property (nonatomic, weak) IBOutlet UILabel *title;
@property (nonatomic, weak) IBOutlet UILabel *authorComments;
@property (nonatomic, weak) IBOutlet UILabel *time;
@end
Run Code Online (Sandbox Code Playgroud)
post.m:
#import "post.h"
@implementation post
@synthesize title = _title;
@synthesize authorComments = _authorComments;
@synthesize time = _time;
@end
Run Code Online (Sandbox Code Playgroud)
我的nib文件的图像:

所以现在我的nib文件中的所有东西都链接到我的帖子类,除了实际的单元格本身因为我被告知我必须用"视图"链接它但我不确定如何(我尝试将它链接到backgroundView但是这样做了不解决我的问题).我也试过给实际的细胞对象上课,但它不能解决我的问题.
然后在我的控制器中,我有以下代码:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
// this is the identifier that I gave the table cell within my nib
static NSString *simpleTableIdentifier = @"post";
// grabs me a cell to use
post *cell = (post *) [tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier];
if (cell == nil) {
cell = [[post alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:simpleTableIdentifier];
}
// sets the text on one of the labels within my cell to something else
cell.title.text = @"this is a test";
// returns my customized cell
return cell;
}
Run Code Online (Sandbox Code Playgroud)
但是,当我运行我的应用程序时,我得到这个:

我知道我遇到的问题与我的自定义单元格有关,因为我之前已将它工作,当我将笔尖上的文件所有者设置为我的控制器时,而不是尝试我正在做的现在正在尝试创建UITableViewCell的子类.我想要做的就是能够创建一个包含多个不同标签的自定义单元格,然后能够以编程方式编辑单元格上的文本.
如果它有帮助,这里是我的所有源文件和我的nib文件:
您必须在您的UITableView中注册NFS:forCellReuseIdentifier:类似这样:
- (void)viewDidLoad {
[super viewDidLoad];
[self.tableView registerNib:[UINib nibWithNibName:@"post" bundle:[NSBundle mainBundle]] forCellReuseIdentifier:@"post"];
}
Run Code Online (Sandbox Code Playgroud)
迅速:
override func viewDidLoad() {
super.viewDidLoad()
tableView.registerNib(UINib(nibName: "post", bundle: NSBundle.mainBundle()), forCellReuseIdentifier: "post")
}
Run Code Online (Sandbox Code Playgroud)
你必须做两件事
在xib文件中
DisConnect文件所有者的所有插座,并将插座连接到您的自定义单元对象
在代码中
从笔尖加载单元格
if (cell == nil) {
//cell = [[post alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:simpleTableIdentifier];
cell = [[[NSBundle mainBundle]loadNibNamed:@"post" owner:self options:nil]objectAtIndex:0];
}
Run Code Online (Sandbox Code Playgroud)