2013-03-17 44 views
0

我想我現在真的很接近,但我錯過了一些東西,如果我在tableView:cellForRowAtIndexPath方法中返回單元格之前中斷了,單元格保存了正確的數據,但它沒有出現在表格中。我假設我的UITableView不掛接到的UIViewController正確如何讓定製UITableViewCell出現在自定義UIViewController與故事板的UITableView中?

這裏是我的自定義單元格:SCCustomerTableCell.h

#import <UIKit/UIKit.h> 
#import "SCCustomer.h" 

@interface SCCustomerTableCell : UITableViewCell 

@property (strong, nonatomic) IBOutlet UILabel *companyLabel; 
-(void)configureCellWithCustomer:(SCCustomer *)customer; 

@end 

SCCustomerTableCell.m

#import "SCCustomerTableCell.h" 

@implementation SCCustomerTableCell 

-(void)configureCellWithCustomer:(SCCustomer *)customer 
{ 
    self.companyLabel = [[UILabel alloc] init]; 
    self.companyLabel.text = [customer dbaName]; 
} 

@end 

而定製視圖公司ntroller:SCCustomersVC.h

#import <UIKit/UIKit.h> 
#import "SCCustomerTableCell.h" 
#import "SCDataObject.h" 

@interface SCCustomersVC : UIViewController <UITableViewDelegate, UITableViewDataSource, UISearchBarDelegate> 

@property (strong, nonatomic) IBOutlet UITableView *customersTable; 
@property SCDataObject *dataObject; 
@property (nonatomic, strong) NSArray *customers; 

@end 

SCCustomersVC.m

#import "SCCustomersVC.h" 
#import "SCSplitVC.h" 
#import "SCDataObject.h" 

@interface SCCustomersVC() 
@end 

@implementation SCCustomersVC 

@synthesize customersTable; 

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView 
{ 
    return 1; 
} 

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section 
{ 
    return [self.customers count]; 
} 

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    static NSString *CellIdentifier = @"CustomerCell"; 
    [self.customersTable registerClass:[SCCustomerTableCell class] forCellReuseIdentifier:CellIdentifier]; 
    SCCustomerTableCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath]; 
    SCCustomer *customer = (SCCustomer *)[self.customers objectAtIndex:indexPath.row]; 
    [cell configureCellWithCustomer:customer]; 
    return cell; 
} 

編輯:我忘了故事板連接的屏幕:
SCCustomersVCUITableViewSCCustomerTableCellUILabel

+0

您是否將SCCustomerVC設置爲表的數據源和委託?另外,您不應該在cellForRowAtIndexPath中註冊該類 - 它會多次調用,並且只需要註冊一次。最好在viewDidLoad中做。 – rdelmar 2013-03-17 03:12:59

+0

我相信他們是通過故事板正確設置的。我添加了屏幕截圖鏈接。注意註冊課程。謝謝。 – guptron 2013-03-17 03:22:13

+0

他們現在,但他們不是當我第一次看。 – rdelmar 2013-03-17 03:24:04

回答

1

好,有幾件事情你做錯了。我從屏幕截圖中看到,您沒有將控制器設置爲表視圖的數據源或委託。你需要這樣做。其次,如果您在IB的表格視圖中創建自定義單元格,則無需註冊該類,只需確保已設置標識符即可。第三,由於你在IB中製作了標籤,並且你有一個插件,所以你不需要在你的單元的.m文件中分配init - 實際上你在.m中完全不需要任何東西。只需在cellForRowAtIndexPath方法中放入cell.companyLabel.text = ...即可。

+0

非常感謝! – guptron 2013-03-17 14:56:42

相關問題