2014-02-23 102 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每个部分的值都相同。