ios - 将数组过滤成部分

标签 ios objective-c uitableview

我正在构建一个基于数组中数据的 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 操作系统的 header 如下 -

#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

行数 我不确定如何计算适用于每个部分的数据行的数量 - 所以这是目前硬编码的 -

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

将数据过滤到表格单元格中

我的问题是我现在不知道如何将数据过滤到行中 - 我目前有以下内容(我的表格 View 中有两种单元格类型) - 当运行代码时,代码会在正确的单元格中显示相关数据 - 但重复每个部分的数据相同 - 如何正确过滤它?

-(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;

      }
 }

最佳答案

我会改变你正在做的事情。我没有一个包含大量项目列表和静态部分计数的数组,而是有一个包含部分的数组,每个部分将是另一个包含行项目的数组。这可以手动创建(您的配置代码会更改)或自动创建(通过迭代现有的 sightsArray 并在其上运行各种谓词。选择实际上取决于您拥有多少项目以及多少/他们将来会从哪里来。

一旦你有了它,部分的数量是 self.sightsArray.count 并且每个部分中的行数是 [self.sightsArray[section] count]一个部分的行是:

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

我还会使用 indexPath.row == 0 作为您应该有一个介绍行的指示,因为为了自动组织这些部分,我会保留所有 siteSection每个部分的值相同。

关于ios - 将数组过滤成部分,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21972543/

相关文章:

ios - 是否可以自定义 Apple iOS TouchID AlertView

ios - 两个 Collection View Cell 之间的转换

ios - 在自定义单元格按钮操作中获取节号

ios - 在 UITableView 中从网络加载图像的更有效方法是什么?

iOS - UITableViewCell,高亮时设置 subview 背景图片

ios - 导航栏位置固定不适用于 IOS 设备

ios - 如何修复 mapKit Ios 中使用的过多内存?

ios - 滚动后 UITableView 单元格正确

objective-c - NSDateComponents 工作日不显示正确的工作日?

objective-c - 来自Object-C类的非指定的初始化程序继承