2013-03-13 57 views
4

我想在同一個xib中使用具有UITableView的自定義UITableviewCell而不創建UITableViewCell類?如何在同一個xib中使用自定義的UITableViewCell和UITableView

正如你可以看到波紋管我設置標識符的UITableViewCell和使用它像這樣:

UITableView and UITableViewCell in same xib

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
    { 
    static NSString *CellIdentifier = @"CustomIdentifier"; 

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 
    if (cell == nil) { 
    cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier]; 
    } 

} 

但不工作?

回答

0

是的,你可以做下面的代碼

在viewDidLoad中或viewWillAppear中

label1Array=[NSMutableArray arrayWithObjects:@"A",@"B",nil]; 
label2Array=[NSMutableArray arrayWithObjects:@"C",@"D",nil]; 
label3Array=[NSMutableArray arrayWithObjects:@"E",@"F",nil]; 

的UITableViewDelegate

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

static NSString *CellIdentifier = @"aCell"; 

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 

if (cell == nil) { 
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier]; 
UILabel *label1=[[UILabel alloc]init]; 
label1.frame=CGRectMake(0,0,40,40); 
label1.text=[label1Array objectAtIndex:indexPath.row]; 
................ 
[cell.contentView addSubView: label1]; 

UILabel *label2=[[UILabel alloc]init]; 
label2.frame=CGRectMake(50,0,40,40); 
label2.text=[label2Array objectAtIndex:indexPath.row]; 
[cell.contentView addSubView: label2]; 

UILabel *label3=[[UILabel alloc]init]; 
label3.frame=CGRectMake(100,0,40,40); 
label3.text=[label3Array objectAtIndex:indexPath.row]; 
[cell.contentView addSubView: label3]; 
} 

希望這有助於!

+0

感謝您的重播,但我想使用一個可視化的UiTableCell,並且無需在xib中爲它創建類,這很容易在故事板中執行此操作,但是我不知道如何在xib文件中執行此操作 – 2013-03-13 07:13:33

+0

@ArashZeinoddini實際上此代碼是隻爲xib ..可以簡要說明你的問題,我不能得到你的問題。 – Dany 2013-03-13 07:14:31

+0

你知道,我想創建一個自定義的UITableViewCell,好嗎?和正常的流程來做到這一點是使它的類,如本教程UITableViewCell:http://www.altinkonline.nl/tutorials/xcode/uitableview/ uitableviewcell /,好嗎?,但我真的不想做自定義的UITableViewCell類(.h/.m),只是想創建一個UITableViewCell在我創建UITableView,好吧,當然我設置了一個標識符使用此創建了UITableViewCell。 – 2013-03-13 07:23:19

3

一個UITableViewCell * customCell屬性添加到您的視圖控制器(例如,您的文件ShowUsers.h)...

@property (nonatomic, strong) IBOutlet UITableViewCell *customCell; 

,並將其連接到您的廈門國際銀行的自定義單元格。然後使用cellForRow中的以下代碼(例如,您的文件ShowUsers.m):

if (cell == nil) { 
    [[NSBundle mainBundle] loadNibNamed:@"NibName" owner:self options:nil]; 
    cell = self.customCell; 
    self.customCell = nil; 
} 
+0

謝謝,但UITableViewCell和UItableView在一個xib中 – 2013-03-13 07:26:09

+0

您可以將tableviewcell移動到單獨的xib。 – jamihash 2013-03-13 07:28:14

+0

這似乎確實完美地工作,2013年11月。 – Fattie 2013-11-01 09:33:05

0

請參閱...您可以這樣做。這裏你使用XIB在一個TableView中添加兩個UITableViewCell。

在Class.h文件中: -

聲明一個用於單元數量的可變數組。

 { 
     NSMutableArray * sectionRows; 
     } 

    @property (nonatomic,retain) IBOutlet UITableViewCell *addTvc1; 
    @property (nonatomic,retain) IBOutlet UITableViewCell *addTvc2; 

在Class.m: -

@synthesize addTvc1, addTvc2; 

    - (void)viewDidLoad 
     { 
     [super viewDidLoad]; 

     sectionRows = [[NSMutableArray alloc] init]; 

     [sectionRows addObject:[[NSMutableArray alloc] 
         initWithObjects:addTvc1, nil]]; 

     [sectionRows addObject:[[NSMutableArray alloc] 
         initWithObjects:addTvc2, nil]]; 

    } 

在的UITableViewDelegate方法 -

添加這 -

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView 
    { 
     return [sectionRows count]; 
    } 

    - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:  
                 (NSInteger)section 
    { 
     return [[sectionRows objectAtIndex:section] count]; 
    } 

    - (UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath: 
                 (NSIndexPath *)indexPath 
    { 
     return [[sectionRows objectAtIndex:indexPath.section] 
           objectAtIndex:indexPath.row]; 
    } 

隨着代碼,在類的XIB下降和拖曳兩根的UITableViewCell這應該在視圖之外,並添加文件所有者的單元格。 它會工作完美。這裏不需要創建一個單獨的自定義UITableViewCell類。

+0

如果您有任何與代碼有關的疑問,請問......? – 2013-03-13 08:11:56

+0

最新花花公子?它在工作.... – 2013-03-13 10:13:18

相關問題