[英]No Visible View in Custom Table View Cell
我正在做一個關於tableView
和tableViewCell
的練習。 而且自定義視圖單元格存在一些問題。
我已經創建了自己的自定義tableViewCell
與名為newCellView.xib
文件。 我需要.h和.m文件,我選擇超類作為UITableViewCell
。
在名為TableViewController
視圖控制器中,我正在使用默認單元格創建一個表,如下所示。
- (UITableViewCell *)tableView: (UITableView *)tableView cellForRowAtIndexPath: (NSIndexPath *)indexPath
{
UITableViewCell *cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:nil];
cell.textLabel.text = [showNames objectAtIndex:[indexPath row]];
cell.imageView.image = [UIImage imageNamed:[iconArray objectAtIndex:[indexPath row]]];
return cell;
}
一旦創建了自己的自定義視圖。 我將newViewCell.h
導入到我的viewController
並更新了這樣的代碼。
- (UITableViewCell *)tableView: (UITableView *)tableView cellForRowAtIndexPath: (NSIndexPath *)indexPath
{
static NSString *newCellViewString = @"newCellView";
newCellView *cell = [tableView dequeueReusableCellWithIdentifier:newCellViewString];
//newCellView *cell = [[newCellView alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:newCellView];
if (cell == nil)
{
NSArray *nib = [[NSBundle mainBundle] loadNibNamed:@"newCellView" owner:self options:nil];
cell = (newCellView *)[nib objectAtIndex:0];
}
但是,當我運行該應用程序時,我看到一個空白頁面,就像我沒有相關視圖一樣。 也許我忘記了要添加的一些連接。 我什至看不到要查看的空白單元格。 只有白色的空白頁。 任何幫助都會很棒。 謝謝。
編輯其他文件
這是我的.h和.m文件。
#import <UIKit/UIKit.h>
@interface newCellView : UITableViewCell <UITableViewDelegate>
@property (nonatomic, strong) IBOutlet UILabel *nameLabel;
@property (nonatomic, strong) IBOutlet UIImageView *iconImageView;
@property (retain, nonatomic) IBOutlet UIButton *extendButton;
@end
.m文件
#import "newCellView.h"
@implementation newCellView
@synthesize nameLabel;
@synthesize extendButton;
@synthesize iconImageView;
- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
if (self) {
// Initialization code
}
return self;
}
- (void)setSelected:(BOOL)selected animated:(BOOL)animated
{
[super setSelected:selected animated:animated];
// Configure the view for the selected state
}
- (void)dealloc {
[extendButton release];
[nameLabel release];
[iconImageView release];
[super dealloc];
}
@end
在處理自定義單元格時,我總是嘗試將與該單元格相關的所有內容封裝在UITableViewCell子類中(包括NIB / outlets / ..),並使用tableView registerClass: forCellReuseIdentifier:
方法來告訴我的表要使用哪個類它的細胞。
在您的示例中,您可以這樣做:
在newCellView.m中,在單元格init中添加筆尖加載:
-(id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
if (self) {
// Initialization code
NSArray *nib = [[NSBundle mainBundle] loadNibNamed:@"newCellView" owner:self options:nil];
self = [nib objectAtIndex:0];
}
return self;
}
確保所有插座連接正確。 (即您的Nib中的UITableViewCell屬於NewCellView類,..)
然后在控制器的viewDidLoad中,告訴您的表對其單元格使用newCellView:
[yourTableView registerClass:[newCellView class] forCellReuseIdentifier:@"newCellView"];
最后在cellForRowAtIndexpath中:
newCellView *cell = [tableView dequeueReusableCellWithIdentifier:@"newCellView"];
if (cell == nil)
{
cell = [[NewCellView alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"newCellView]";
}
我認為,您的問題是自定義單元格未附加到自定義類單元格。 創建UITableViewCell類時,它不會與其一起創建xib。 然后,您需要創建一個xib文件,該文件需要附加到定制類文件中。
創建一個沒有內容的EMPTY NIB文件。 然后,通過對象添加一個uitablecustomcell ,並在添加新對象時轉到GO File Inspector,然后在File Name中輸入名稱newCellView 。 現在,自定義單元格將顯示EMPTY行。
現在,向自定義單元格添加幾個視圖,並通過在.h文件中創建的IBOutlet附加這些視圖,這些文件分別是nameLabel,iconImageView,extendButton。
這是我之前遇到的一個簡單錯誤。 你忘了打字。 它應該是:
cell = (newCellView*)[nib objectAtIndex:0];
如果這不能解決您的問題,請確保已將xib文件設置為使用“ newCellView”類,而不使用默認的“ UITableViewCell”類。 此外,如果您手動創建了一個表視圖,並將其添加為另一個視圖的子視圖,或者將其設置為loadView方法或類似視圖中的視圖,而不是對UITableViewController進行子類化,請確保為表視圖設置框架並將其添加為子視圖。
您可能要從newCellView.h類中刪除。 tableview的委托不應是一個視圖或一個子類。 尤其不是tableview將要顯示的單元格。 UITableViewController應該正在接收委托方法。
我找到了答案。 除了單元格視圖的位置之外,所有代碼在我的代碼中都運行良好。
項目使用自動版式作為默認屬性。 因此,每當我看到白頁時,單元格實際上都超出了范圍。 我禁用了自動布局,並通過大小檢查器設置了項目的移動屬性。
感謝您的努力。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.