2013-10-25 46 views
-1

我有2個簡單的類:在UITableView中動態創建行顯示自定義數據

Class1 { 
    int i; 
    NSString s; 
} 

Class2 { 
    NSString name; 
    NSString age; 
} 

在我的UIViewController我有2 NSMutableArray裏,每列保持Class1的& Class2中的多個對象。

我需要爲每個數組創建一個新的部分,部分中的行數應該等於該數組中的對象數。 另外在每個單元格中我需要顯示每個對象變量的數據。

請幫忙!!!

+0

「請幫忙!!!」並不是一個好的起點。你之前嘗試過什麼?你寫了一些代碼?如果是這樣,你可以把它展示給我們以幫助你嗎? –

回答

0

嘗試下面的代碼...

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView 
{ 

    return 2; 
} 


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section 
{ 
    switch (section) { 
     case 0: 
      return self.class1Array.count; 


     case 1: 
      return self.class2Array.count; 
    } 
} 
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    static NSString *CellIdentifier = @"cell"; 
    UITableViewCell *cell=[tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath]; 

    if(cell==nil) 
    { 
     cell= [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier]; 


    } 
switch(indexPath.section) 
{ 
    case 0: 
    cell.textLabel.text=[self.class1Array objectAtIndex:indexPath.row] s]; 
    cell.detailTextLabel.text=[self.class1Array objectAtIndex:indexPath.row] i]; 
    return cell; 
    case 1: 
    cell.textLabel.text=[self.class2Array objectAtIndex:indexPath.row] name]; 
    cell.detailTextLabel.text=[self.class1Array objectAtIndex:indexPath.row] age]; 
    return cell; 
} 

} 

編輯:

如果你想創建自定義的細胞嘗試做這樣this

+0

聽起來很完美,讓我試試這個...... – user2918922

+0

但是,如果我必須在詳細文本中顯示兩個變量數據,以及如果我有5個數據變量顯示在詳細文本中的情況下。例如,我詳細的文字應該像在這種情況下 1.我的名字\ n 2.我的年齡\ n 3.我的地址\ n 4.我的家鄉\ n – user2918922

+0

@ user2918922您必須創建自定義單元格。 .. – Raon

0

嘗試

if(tableview.section==0) 
{ 
    cell.label.text= [aray1 object at index :indexpath.row] 
} 

else if(tableview.section==1) 
{ 
    cell.label.text= [aray object at index :indexpath.row] 
} 
相關問題