2013-03-26 93 views
2

對不起,再問全問題說明。我有什麼,我有resultsArray其中有標題說明等從服務器獲取,但問題是,我想要顯示這些數據的部分。用單個NSMutableArray填充UITableView部分表

因此可以說,如果有三個部分來自數組,那麼如何使用單個resultArray將數據填充到每個部分。

- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section 
    { 
return [categoryArray objectAtIndex:section]; 
} 

resutArray對所有區中的所有數據,因此如何根據部分顯示

陣列的

結構

   ObjectData *theObject =[[ObjectData alloc] init]; 
      [theObject setCategory:[dict objectForKey:@"category"]]; 
      [theObject setSub_Category:[dict objectForKey:@"sub_Category"]];  
      [theObject setContent_Type:[dict objectForKey:@"content_Type"]]; 
      [theObject setContent_Title:[dict objectForKey:@"content_Title"]]; 
      [theObject setPublisher:[dict objectForKey:@"publisher"]]; 
      [theObject setContent_Description:[dict objectForKey:@"content_Description"]]; 
    [theObject setContent_ID:[dict objectForKey:@"content_ID"]]; 
    [theObject setContent_Source:[dict objectForKey:@"content_Source"]]; 
     [resultArray addObject:theObject]; 
+0

請問你會展示你的數組的結構,你存儲了哪些數據。以及您想要在部分中展示的項目。 – 2013-03-26 07:07:50

+0

@DipenPanchasara我已添加數組的結構 – 2013-03-26 07:11:54

+0

顯示您的代碼,然後輕鬆解決.Thansk – moosa0709 2013-03-26 08:26:24

回答

0

如果你已經是一個NSMutableArray,你想拆呢分成部分意味着你必須知道對象的索引。 例如,

[ 
Section 1, 
Section1, 
Section1, 
Section 2, 
Section 2, 
Section2, 
Section 3, 
Section 3, 
Section 3 
] 

在第一部分從6至8顯示索引數據從0到2的索引,在第二從3至5的索引,在第三。

其他更好去NSDictionary。將章節標題保留爲鍵,並將值保存爲Dictionary中的NSArray。所以加載單元格會更容易。

你的NSArray對象數據的。所以,你可以做到這一點作爲

NSMutableDictionary *sectionDict = [[NSMutableDictionary alloc] init]; 

    for(int i=0; i < [data count]; i++) 
    { 
ObjectData *theObject = [data objectAtIndex:i]; 
     NSMutableArray *arr; 
     if([sectionDict valueForKey:theObject.category]) 
     { 
      arr = [sectionDict valueForKey:theObject.category]; 
     } 
     else 
     { 
      arr = [[NSMutableArray alloc] init]; 
     } 
     [arr addObject:theObject]; 
     [sectionDict setObject:arr forKey:theObject.category]; 
    } 

所以,現在你將需要的數據...

+0

是的部分標題將是最好的,使關鍵和負載數據任何例如如何做到這一點,請 – 2013-03-26 07:11:34

+0

查看更新的答案..您已拆分數據基於索引,應該有所有部分單獨的密鑰... – 2013-03-26 07:18:05

+0

我沒有得到這可以添加我的代碼,因爲我已經更加明確請 – 2013-03-26 07:22:05

1

使用自定義類,你可以做到這一點。

myClass.h

@interface myClass : NSObject 
@property(nonatomic,retain) NSString* sectionHeader; 
@property(nonatomic,retain) NSMutableArray* sectionRows; 
@end 

myClass.m

#import "myClass.h" 

@implementation myClass 
@synthesize sectionHeader; 
@synthesize sectionRows; 
@end 

首先,您需要設置與上面的類對象的resultArray。爲課堂設置合適的價值。例如:

myClass *myClassObj = [[myClass alloc] init]; 
myClassObj.sectionHeader = @"Your section title"; 
myClassObj.sectionRows = < Your NSMutableArray for rows under current section >; 
[retunArray addObject:myClassObj]; 

立即設置numberOfSectionsInTableViewnumberOfRowsInSectiontitleForHeaderInSectioncellForRowAtIndexPath

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

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

- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section 
{ 
    myClass *myClassObject = [returnArray objectAtIndex:section]; 
    return myClassObject.sectionHeader; 
} 

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    static NSString *CellIdentifier = @"Cell"; 
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 
    if (cell == nil) 
    { 
     cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease]; 
    } 

    myClass *myClassObject = [returnArray objectAtIndex:indexPath.section]; 
    NSLog(@"%@",[myClassObject.sectionRows objectAtIndex:indexPath.row]); 

    return cell; 
} 
+0

但它將separte每個部分數據不同的標題 – 2013-03-26 07:14:08

+0

你是正確的讓我說我在數組中具有標題部分並且resultArray包含所有數據如何將每個部分的數據從resultArray中分離 – 2013-03-26 07:17:20

+0

再次查看更新 – 2013-03-26 07:18:52

0

做相同的代碼示例

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView 
{ 
    // Return the number of sections. 
    return [sectionArray count]; 
} 

-(NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section 
{ 
//Do your stuff 
} 
+0

我不知道一段時間,它可能是10的確切數量一段時間多一點或更少10個數組來自服務器 – 2013-03-26 08:36:42

+1

使用[Array count]; – moosa0709 2013-03-26 08:38:41

+0

你可以編輯計數 – 2013-03-26 08:41:38

2

看的像你有目標數據,其具有的屬性稱爲類,並且你想用ObjectData按類別分組來顯示錶格視圖。 您可以這樣做,生成一個key = unique類別的NSDictionary,並且value =該類別的ObjectData的NSArray。然後使用該NSDictionary作爲數據源的數據。

NSArray *tempArray =[[DataController staticVersion] startParsing:@"http://www.celeritas-solutions.com/pah_brd_v1/productivo/catalogMaster.php"]; 
// dictionary for cells, you'll probably want this to be ivar or property 
NSMutableDictionary* dictionary = [NSMutableDictionary dictionary]; 
// array for headers, you'll probably want this to be ivar or property 
NSMutableArray* categories = [NSMutableArray array]; 

for (int i = 0; i<[tempArray count]; i++) { 

    id *item = [tempArray objectAtIndex:i]; 
    NSDictionary *dict = (NSDictionary *) item; 
    ObjectData *theObject =[[ObjectData alloc] init]; 
    [theObject setCategory:[dict objectForKey:@"category"]]; 
    [theObject setSub_Category:[dict objectForKey:@"sub_Category"]]; 
    [theObject setContent_Type:[dict objectForKey:@"content_Type"]]; 
    [theObject setContent_Title:[dict objectForKey:@"content_Title"]]; 
    [theObject setPublisher:[dict objectForKey:@"publisher"]]; 
    [theObject setContent_Description:[dict objectForKey:@"content_Description"]]; 
    [theObject setContent_ID:[dict objectForKey:@"content_ID"]]; 
    [theObject setContent_Source:[dict objectForKey:@"content_Source"]]; 


    [resultArray addObject:theObject]; 
    [theObject release]; 
    theObject=nil; 

    // here you populate that dictionary and categories array 
    NSMutableArray* objs = [dictionary objectForKey:theObject.category]; 
    if(!objs) 
    { 
     objs = [NSMutableArray array]; 
     [dictionary setObject:objs forKey:theObject.category]; 
     [categories addObject:theObject.category]; 
    } 
    [objs addObject:theObject]; 
} 

然後在你的控制器:

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

    return [categories count] ; 

} 

- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section 
{ 

     return [categories objectAtIndex:section]; 
} 



- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    static NSString *CellIdentifier = @"CellID"; 
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 
    if (cell == nil) 
    { 
    cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue2 reuseIdentifier:CellIdentifier]; 
    } 

    NSString* category = [categories objectAtIndex: indexPath.section]; 
    NSMutableArray* objs = [dictionary objectForKey:category]; 
    ObjectData* obj = [objs objectAtIndex: indexPath.row]; 

    // populate cell here 

} 

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { 

    NSString* category = [categories objectAtIndex:section]; 
    NSMutableArray* objs = [dictionary objectForKey:category]; 
    return [objs count]; 
} 

請注意,這只是一個例子,你可以爲你想要優化這個。

+0

是的你是對的我想這樣可以請你添加一些代碼,如果你可以如何添加像這樣 – 2013-03-26 08:42:21

+0

然後如何顯示行數和單元格的值 – 2013-03-26 08:46:49

+0

而不是類別我認爲我們必須給resultArray其中結果是從json – 2013-03-26 08:50:36

0

您可能會發現使用免費的Sensible TableView框架更容易。框架只需要你的數組並生成所有的表格視圖單元格和部分,如果你願意的話,包括所有的細節視圖。我覺得它更簡單,並且需要幾分鐘時間才能實現。

+0

你能分享一些鏈接代碼或任何期待 – 2013-03-26 09:20:23

+0

http://sensiblecocoa.com – Matt 2013-03-26 09:28:58