我正在根据数组中的数据构建一个IOS应用程序 - 我正在努力将数据过滤成行 - 我的数组的片段如下 -
ARRAY数据/数组 -
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的标头如下 -
#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 =@"sightsCell";
static NSString *CellIdentifierH =@"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 :(得分:1)
我会改变你正在做的事情。不是让一个数组包含大项目列表和部分的静态计数,而是我有一个包含部分的数组,每个部分都是包含行项目的另一个数组。这可以手动创建(您的配置代码会更改)或自动创建(通过迭代现有的sightsArray
并在其上运行各种谓词。选择实际取决于您拥有多少项目以及它们将具有多少/多少来自未来。
完成后,部分的数量为self.sightsArray.count
,每个部分的行数为[self.sightsArray[section] count]
,部分的行为:
sightsObject *b = self.sightsArray[indexPath.section][indexPath.row];
我还会使用indexPath.row == 0
表示您应该有一个介绍行,因为要自动组织这些部分,我会保留每个部分的所有siteSection
值相同。