2011-11-23 33 views
0

我有點困惑。我試圖創建一個自定義單元格,我想使用界面生成器的方式。在UITableView中使用UITableViewCell

我創建一個表正常的方法是有作爲表:

.H

@interface AssessList : UIViewController { 

    IBOutlet UITableView *tblAssessList; 
} 

@property(nonatomic, retain) UITableView *tblAssessList; 

@end 

.M

- (NSInteger) 
numberOfSectionsInTableView:(UITableView *)tableView { 
    return groupArray.count; 
} 


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { 
    return totalArray.count; 
} 


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { 

    static NSString *CellIdentifier = @"Cell"; 

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 
    if (cell == nil) { 
     cell = [[[UITableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier:CellIdentifier] autorelease]; 
    } 



    cell.textLabel.text = @"I am the text...."; 

     return cell; 
} 

現在我已經創建了一個新類的細胞,我想我明白如何把它。但我可以離開.h作爲

@interface AssessList : UIViewController 

還是用整個表上的類/筆尖是否是UITableViewController?

湯姆

回答

5

做類/筆尖與它的全表必須是 UITableViewController?

號公報UITableViewController僅僅是一個方便UIViewController子類,其具有UITableView和已經設置作爲其代表/數據源(它被聲明爲符合UITableViewDelegateUITableViewDatasource協議),它也已預先填充方法在Xcode爲您生成的模板實現文件中實現這些協議。你可以自己做所有這些,我經常這樣做。

然而,您應該爲您的UITableViewCell製作一個IBOutlet,以便您可以從nib文件加載它(請參閱Table View Programming Guide中的Loading Custom Table-View Cells From Nib Files)。

+0

您是否必須將UITableView筆尖的數據源/委託(所以實際表)設置爲filesowner仍然? – TMB87

+0

您可以在IB(檢查fileowner類設置爲AssessList)或代碼(在AssessList viewDidLoad中,如下所示:tblAssessList.delegate = self; tblAssessList.datasource = self; – jbat100

0

我想這應該是子類的UITableViewCell

@interface AssessList : UITableViewCell 
+0

這是完整的表,你說的全表應該是的UITableViewCell的子類或類我已經爲自定義單元格中創建,這應該是一個子類? – TMB87

+0

我認爲這是你的UITavleViewCell .....在這種情況下,它應該由UITableViewController繼承...但仍然不確定 –

+0

TomBeech代碼行@interface AssessList:UIViewController是完美的,因爲它是具有表視圖的UIViewController子類作爲它的子視圖而不是文件的所有者 –

0

的。當你想你也需要的UITableViewCell的子類定製的tableview細胞..

可以在這裏找到教程blog

請記住,可以在不創建自定義單元格的情況下完成很多操作,包括添加開關以使tableview看起來像來自settings.app的那個,以及iPod顯示歌曲的方式。

+0

謝謝你,我會讀一讀。我試圖實現的是每張照片下的一張照片和一些文字。這是否必須在自定義類中? – TMB87

0

在assessList類您使用的是otherviewController(UITableViewCell的子類)創建的自定義單元格,所以沒有必要改變這一行

@interface AssessList:UIViewController中

注: - otherviewController應UITableViewCell的一個子類

1

如果你想在Interface Builder Way中做,然後創建一個xib(查看xib)。從obj面板中拖放一個UITableViewCell對象。根據需要自定義它。在的tableView:的cellForRowAtIndexPath:方法,這樣做:

UITableViewCell * aCell = [tableview dequeueReusableCellWithIdentifier:@"SomeIdentifier"]; 
if (aCell == nil) 
{ 


    NSArray *arr = [[NSBundle mainBundle] loadNibNamed:@"CustomCellNibName" owner:self options:nil]; 

    for (NSObject *anObj in arr) { 

     if([anObj isKindOfClass:[UITableViewCell class]]) { 

      aCell = (UITableViewCell *)anObj; 

     } 
    } 
} 

用於tableviewcell標識符可以在IB進行設置。

1

你可以用這個示例代碼描述聽到。

http://ijoshsmith.com/2011/07/16/creating-a-custom-uitableviewcell-in-ios-4/ 

或者你可以使用這個鏈接也

http://cocoawithlove.com/2009/04/easy-custom-uitableview-drawing.html 
相關問題