2011-04-26 69 views
1

我如何在tableview中劃分部分?下面是我試圖分裂,但無法獲得部分的代碼..如何分割UITableView中的部分?

#import "ipapbatch3ViewController.h" 

@implementation ipapbatch3ViewController  

- (NSInteger)tableView:(UITableView *)table numberOfRowsInSection:(NSInteger)section 
{ 
    NSInteger numberofrows=5;  
    return numberofrows; 

} 

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath  
{ 
    NSArray *a = [NSArray arrayWithObjects:@"rams",@"balu",@"praveen",@"sagar",@"faizan",nil];  
    UITableViewCell * r = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:nil];  
    [r autorelease]; 

    r.textLabel.text=[a objectAtIndex:[indexPath row]]; 

         return r; 
         } 
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView  
{  
    NSInteger sc = 2;  
    return sc;  

}  




@end 

回答

3

這一切都在的cellForRowAtIndexPath,這裏有一個小例子:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath  
{ 
    UITableViewCell * r = [[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:nil] autorelease]; 

    if (indexPath.section == 0) { 
     NSArray *a = [NSArray arrayWithObjects:@"a",@"b",@"c",@"d",@"e",nil];  
     r.textLabel.text = [a objectAtIndex:indexPath.row]; 

    } else { 
     NSArray *b = [NSArray arrayWithObjects:@"1",@"2",@"3",@"4",@"5",nil];  
     r.textLabel.text = [b objectAtIndex:indexPath.row]; 

    } 

    return r; 
} 
+0

好例子展示瞭如何把一個表分成部分,但它是壞的內存管理。您創建了大量的Autoreleased陣列。這些數組應該創建一次並在dealloc中釋放。 – Cyprian 2011-04-26 07:53:13

+1

@Cyprian,OP問如何分段,我採用了他的代碼。我認爲你不明白自動釋放對象是什麼。只要你不保留它們,你就不擁有自動釋放的對象,因此你不會在任何地方釋放它們。 – 2011-04-26 07:56:47

+0

@Nick Weaver如果你正確地閱讀我的評論我說你確實釋放數組,如果你在init中顯式創建它。用粗糙的代碼糾正粗糙的代碼會讓人們在後面提出更多問題,所以最好指出他們一直沿着一個良好的方向。 – Cyprian 2011-04-26 09:23:39

0

上面的代碼中你的主要問題是:你永遠不會知道哪個部分浸入新的細胞,因此他們都陷入了第一個細胞。查看cellForRowAtIndexPath方法,您需要指定新創建的單元格轉到特定的部分。

Go through those example programs provided by apple.

+0

感謝您的建議,現在我明白了什麼是問題.. – 2011-04-27 04:55:14

+0

@toddler在objective-c中:如果解決了問題,您應該將此帖標記爲寫回答,也許您只是向我們展示解決方案。 – 2013-04-23 17:16:41