[英]Storyboard add a custom view which has xib file has no subviews
我用xib文件創建了一個自定義UIView
A,可以通過以下方式正確加載它:
NSArray *starsNib = [[NSBundle mainBundle] loadNibNamed:@"nibName" owner:nil options:nil];
A *starsView = starsNib[0];
[self.view addSubview:starsView];
然后在我的情節提要文件中,將此自定義視圖A添加到UITableViewCell
的contentView
。 加載tableview時,我發現定制視圖A沒有xib文件中添加的子視圖。 我還發現當A的initWithCoder:
返回時,它沒有子視圖。
順便說一句,在xib文件中,我已將parent view
和file's owner
都設置為自定義視圖A類。
我想知道為什么會這樣,為什么initWithCoder:
返回時未加載子視圖?
因為視圖是從情節提要而不是XIB加載的。 在情節提要中,視圖為空。
您應該從情節提要中刪除視圖並將其加載到代碼中,這可能是在實例化單元格時進行的,因為每個單元格只需執行一次即可。
您可以使用Xib創建自定義tableViewCell,Xib具有您的自定義UITableViewCell類的文件所有者,然后您可以使用cellForRowAtIndexPath:
方法,例如
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"Cell"];
if (cell == nil) {
// Load the top-level objects from the custom cell XIB.
NSArray * starsNib = [[NSBundle mainBundle] loadNibNamed:@"nibName" owner:self options:nil];
cell = starsNib[0];
}
return cell;
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.