2014-02-23 132 views
1

我建立基於一個陣列內的數據的應用程序IOS - 我與濾波數據掙扎到行 - 我的陣列的一個片段是如下 -濾波陣列段

陣列數據/陣列 -

sightsObject *sight2 = [[sightsObject alloc] initWithsiteTitle:@"The Beatles Homes" siteSection:@"beatles" siteType:@"Take a glimpse of the fab 4's childhood homes.." siteSubTitle:@"" siteDescription:@"" siteMapUrl:@"" sitePic:@"fabs"]; 

sightsObject *sight3 = [[sightsObject alloc] initWithsiteTitle:@"The Beatles Gig Venues" siteSection:@"beatles" siteType:@"" siteSubTitle:@"Its not all about the Cavern..." siteDescription:@"" siteMapUrl:@"" sitePic:@"fabs"]; 

sightsObject *sight4 = [[sightsObject alloc] initWithsiteTitle:@"The Beates Locations" siteSection:@"beatles" siteType:@"" siteSubTitle:@"Stawberry Fields, Penny Lane, Palm House..." siteDescription:@"" siteMapUrl:@"docks" sitePic:@"fabs"] 

sightsObject *sight5 = [[sightsObject alloc] initWithsiteTitle:@"Albert Dock" siteSection:@"dock" siteType:@"" siteSubTitle:@"" siteDescription:@"" siteMapUrl:@"" sitePic:@""]; 

sightsObject *sight6 = [[sightsObject alloc] initWithsiteTitle:@"Keiths Wine Bar" siteSection:@"Restaurants" siteType:@"" siteSubTitle:@"Classic Eatery on Lark Lane" siteDescription:@"" siteMapUrl:@"" sitePic:@""]; 

self.sightsArray = [NSArray arrayWithObjects: sight2, sight3, sight4, sight5, sight6,nil]; 

SightsObject.H 用於SightsObject OS頁眉如下 -

#import <Foundation/Foundation.h> 

@interface sightsObject : NSObject 


@property(strong)NSString *siteTitle; 
@property(strong)NSString *siteSection; 
@property(strong)NSString *siteType; 
@property(strong)NSString *siteSubTitle; 
@property(strong)NSString *siteDescription; 
@property(strong)NSString *siteMapUrl; 
@property(strong)UIImage *sitePic; 


-(id)initWithsiteTitle:(NSString *)siteTitleD siteSection:(NSString *)siteSectionD siteType:(NSString *)siteTypeD siteSubTitle:(NSString *)siteSubTitleD siteDescription:(NSString *)siteDescriptionD siteMapUrl:(NSString *)siteMapUrlD sitePic:(NSString *)sitePicD; 

@end 

ROWS 我不確定如何計算數據行的,適用於每個部分的金額 - 所以這是目前硬編碼 -

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView 
{ 
    return 8; 
} 

過濾數據加入表格單元格

我的問題是我現在不知道如何將數據過濾到行中 - 我目前有下面的代碼(在我的tableview中有兩種單元格類型) - 運行時代碼在正確的單元格中顯示相關數據 - 但爲每個部分重複相同的數據 - 我如何正確地過濾它?

-(UITableViewCell *)tableView:(UITableView *)tableView 
    cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
static NSString *CellIdentifier [email protected]"sightsCell"; 
static NSString *CellIdentifierH [email protected]"headerCell"; 
NSString * intro = @"intro"; 
NSString * ArtNoP = @"lower"; 

sightsObject *b = [self.sightsArray objectAtIndex:indexPath.row]; 
tableView.tableFooterView = [[UIView alloc] initWithFrame:CGRectZero]; 
    UITableViewCell *cell; 



if ([b.siteSection isEqualToString:intro]) { 
    cell= [tableView dequeueReusableCellWithIdentifier:CellIdentifierH forIndexPath:indexPath]; 
    HeaderCell *cellHead = (HeaderCell *)cell; 
    cellHead.selectionStyle = UITableViewCellSelectionStyleNone; 
    cellHead.sightsText.text = b.siteTitle; 
    cellHead.textLabel.backgroundColor=[UIColor clearColor]; 
    return cellHead; 


} 

else{ 

    cell= [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath]; 
    sightsCell *cellSights = (sightsCell *)cell; 
    cellSights.selectionStyle = UITableViewCellSelectionStyleNone; 
    cellSights.sightsTitle.text = b.siteTitle; 
    cellSights.sightsSubTitle.text = b.siteSubTitle; 
    cell.textLabel.backgroundColor=[UIColor clearColor]; 
    return cellSights; 

     } 
} 
+0

什麼是「正確的」?從你的源數據來看,應該在哪裏?顯示您的'sightsObject'類的標題。 – Wain

+0

Hi @Wain Header added above - 我的意思是我在表格中有兩個自定義類 - 我通過上面的if語句進入 - 這部分工作正常 - 我的問題是它顯示了一系列帶有部分頭和每個相同的數據 - 我想能夠過濾數據到部分 - 然後決定它應該出現在哪個自定義單元格 – Dancer

+0

而且重要的一塊信息是「siteSection」在當前在你的景點陣列中的每個對象陣列'?介紹項目從哪裏來?爲什麼不在'indexPath.row == 0'時添加簡介部分? – Wain

回答

1

我會改變你在做什麼。而不是有一個數組的大項目和靜態數量的部分我有一個數組包含部分,每個部分將是另一個數組包含行項目。這可以手動創建(您的配置代碼會改變),也可以自動創建(通過遍歷現有的sightsArray並在其上運行各種謂詞)。選擇取決於您擁有多少物品以及物品來自。未來

一旦你的,部分的數量爲self.sightsArray.count和行的每個部分中的數字是[self.sightsArray[section] count]以及一個用於該行是:

sightsObject *b = self.sightsArray[indexPath.section][indexPath.row]; 

我也將使用indexPath.row == 0作爲指示你應該有一個介紹行,因爲自動組織這些部分我會保留所有的siteSection每個部分的值都相同。