【问题标题】:UiTableView - Add content to relevant sectionsUiTableView - 将内容添加到相关部分
【发布时间】:2013-12-03 15:48:42
【问题描述】:

首先我要说我是 IOS 开发的新手!

我有一个看起来像这样的数据数组 -

    shopObjects *shop2 = [[shopObjects alloc] 
                initWithshopITitle:@"3 for 2 PT Sessions" 
                shopIGroup:@"standard" 
                shopIDesc:@"Special Offer - Get 3 bespoke PT sessions for the price of just one!" shopIPrice:@"00.20"  
                offer:false];

shopIgroup 字段显示部分名称。

我已通过以下方式将数据列出到表格视图中 -

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
// NOT SURE HOW TO CALCULATE THIS!
return 3;
}

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

 // NOT SURE HOW TO CALCULATE THIS!
return 0;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
return 10; 
}


-(UITableViewCell *)tableView:(UITableView *)tableView
    cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
   static NSString *CellIdentifier =@"shopItems";
   shopObjects *s = [self.shopArray objectAtIndex:indexPath.row];
   UITableViewCell *cell = [tableView
                         dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    cell.textLabel.text=s.shopITitle;
    cell.textLabel.backgroundColor=[UIColor clearColor];
 return cell;
}

我的问题是 -

  1. 如何计算上述方法中的return语句所需的标题数和每个部分的行数?
  2. 然后如何将我的单元格应用于相关部分?

更新 我的 ShopObjects.m 文件供参考 -

@implementation shopObjects

-(id)initWithshopITitle:(NSString *)shopITitleD shopIGroup:(NSString *)shopIGroupD   shopIDesc:(NSString *)shopIDescD shopIPrice:(NSString *)shopIPriceD offer:(Boolean )offerD{
 self= [super init];
  if(self){
    self.shopITitle = shopITitleD;
    self.shopIGroup = shopIGroupD;
    self.shopIDesc = shopIDescD;
    self.shopIPrice = shopIPriceD;
    self.offer = (offerD);



  }
   return self;


 }

ShopObjects.h 文件

进口

@interface shopObjects : NSObject



@property(strong) NSString *shopITitle;
@property(strong) NSString *shopIGroup;
@property(strong) NSString *shopIDesc;
@property(strong) NSString *shopIPrice;
@property Boolean offer;


-(id)initWithshopITitle:(NSString *)shopITitleD shopIGroup:(NSString *)shopIGroupD shopIDesc:(NSString *)shopIDescD shopIPrice:(NSString *)shopIPriceD offer:(BOOL )offerD;


@end

阵列设置

    shopObjects *shop1 = [[shopObjects alloc] initWithshopITitle:@"1 x PT Session"     shopIGroup:@"running" shopIDesc:@"1 x 1hour fitness session with Zoe - bespoke fitness session.\n\nZoe's sessions focus on your general health and fitness so she will look at a bespoke fitness schedule that encompasses exersize, healthy eating, sleep and general lifestyle - to help you meet your goals.\n\nBe good to yourslef and give it a go.." shopIPrice:@"10.00"  offer:false];



  shopObjects *shop2 = [[shopObjects alloc] initWithshopITitle:@"3 for 2 PT Sessions" shopIGroup:@"offers" shopIDesc:@"Special Offer - Get 3 bespoke PT sessions for the price of just one!" shopIPrice:@"00.20"  offer:false];



  shopObjects *shop3 = [[shopObjects alloc] initWithshopITitle:@"1 x Running Club" shopIGroup:@"running" shopIDesc:@"1 x running club session - roup event - max group of 10." shopIPrice:@"15.00"  offer:false];


 shopObjects *shop4 = [[shopObjects alloc] initWithshopITitle:@"1 x Weights session" shopIGroup:@"weights" shopIDesc:@"1 x body building sesson" shopIPrice:@"10.90"  offer:false];



  self.shopArray = [NSArray arrayWithObjects:shop1, shop2, shop3,shop4,  nil];
  self.sectionData = [self.shopArray valueForKeyPath:@"@distinctUnionOfObjects.shopIGroup"];

更新

对于 Greg - 我已经实现了你的代码和建议,但应用仍然崩溃,我现在得到 -

NSLOG - 2013-12-05 10:16:18.595 blah[3321:70b] 你好的值 = 0 2013-12-05 10:16:18.596 blah [3321:70b] hello = 1 的值 2013-12-05 10:16:18.596 blah[3321:70b] hello = 2 的值 2013-12-05 10:16:27.962 等等[3321:70b] 部分:3 2013-12-05 10:16:27.962 blah[3321:70b]-[NSCFConstantString shopIGroup]:无法识别的选择器发送到实例 0x1c7c38 2013-12-05 10:16:27.965 blah[3321:70b] *** 由于未捕获的异常“NSInvalidArgumentException”而终止应用程序,原因:“-[__NSCFConstantString shopIGroup]:无法识别的选择器发送到实例 0x1c7c38” *** 首先抛出调用堆栈: ( 0 核心基础 0x02bde5e4 __exceptionPreprocess + 180 1 libobjc.A.dylib 0x029618b6 objc_exception_throw + 44 2核心基础0x02c7b903-[NSObject(NSObject)不识别选择器:]+275 3 核心基础 0x02bce90b ___forwarding_ + 1019 4 核心基础 0x02bce4ee _CF_forwarding_prep_0 + 14 5 废话 0x000198fb -[shopTVcontroller tableView:numberOfRowsInSection:] + 187 6 UIKit 0x00a33dc0-[UISectionRowData refreshWithSection:tableView:tableViewRowData:] + 2510 7 UIKit 0x00a39384-[UITableViewRowData rectForFooterInSection:heightCanBeGuessed:] + 288 8 UIKit 0x00a39495-[UITableViewRowData heightForTable] + 68 9 UIKit 0x008ae181-[UITableView _updateContentSize] + 400 10 UIKit 0x008c9333-[UITableView setContentInset:] + 329 11 UIKit 0x008efeba-[UIViewController _setNavigationControllerContentInsetAdjustment:] + 538 12 UIKit 0x0091a9bc-[UINavigationController _computeAndApplyScrollContentInsetDeltaForViewController:] + 399 13 UIKit 0x0091ab2e-[UINavigationController _layoutViewController:] + 64 14 UIKit 0x0091b02b-[UINavigationController _updateScrollViewFromViewController:toViewController:] + 235 15 UIKit 0x0090f8ff -[UINavigationController _startCustomTransition:] + 2087 16 UIKit 0x0091c0c7-[UINavigationController _startDeferredTransitionIfNeeded:] + 688 17 UIKit 0x0091ccb9 -[UINavigationController viewWillLayoutSubviews] + 57 18 UIKit 0x00a56181 -[UILayoutContainerView 布局子视图] + 213 19 UIKit 0x0084c267-[UIView(CALayerDelegate) layoutSublayersOfLayer:] + 355 20 libobjc.A.dylib 0x0297381f -[NSObject performSelector:withObject:] + 70 21 QuartzCore 0x0221a2ea -[CALayer layoutSublayers] + 148 22 石英核心 0x0220e0d4 _ZN2CA5Layer16layout_if_neededEPNS_11TransactionE + 380 23 石英核心 0x0220df40 _ZN2CA5Layer28layout_and_display_if_neededEPNS_11TransactionE + 26 24 石英核心 0x02175ae6 _ZN2CA7Context18commit_transactionEPNS_11TransactionE + 294 25 石英核心 0x02176e71 _ZN2CA11Transaction6commitEv + 393 26 石英核心 0x02177544 _ZN2CA11Transaction17observer_callbackEP19__CFRunLoopObservermPv + 92 27 核心基础 0x02ba64ce __CFRUNLOOP_IS_CALLING_OUT_TO_AN_OBSERVER_CALLBACK_FUNCTION + 30 28 核心基础 0x02ba641f __CFRunLoopDoObservers + 399 29 核心基础 0x02b84344 __CFRunLoopRun + 1076 30 核心基础 0x02b83ac3 CFRunLoopRunSpecific + 467 31 核心基础 0x02b838db CFRunLoopRunInMode + 123 32 图形服务 0x03c349e2 GSEventRunModal + 192 33 图形服务 0x03c34809 GSEventRun + 104 34 UIKit 0x007e1d3b UIApplicationMain + 1225

36  libdyld.dylib                       0x0314f70d start + 1
)
libc++abi.dylib: terminating with uncaught exception of type NSException
(lldb) 

错误画面 -

【问题讨论】:

    标签: ios objective-c uitableview


    【解决方案1】:

    您可以通过从数组中获取对象的不同部分来获取部分。最好的方法是添加新的 NSArray 类型的 @property(稍后您将需要它),例如 sectionData 并在 viewDidLoad 中加载不同的值:

    self.sectionData = [self.shopArray valueForKeyPath:@"@distinctUnionOfObjects.shopIGroup"];
    

    接下来,您可以只显示一些不同的对象,从而为您提供多个部分

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

    要获得每个部分的行数,您必须像这样过滤部分名称的数组:

    - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
    {
        //Get your section name from distinct array
        NSString *s = self.sectionData[section]
        //Filter your main array
        NSPredicate *pred = [NSPredicate predicateWithFormat:@"shopIGroup like %@", s];
        NSArray *filteredArr = [self.shopArray filteredArrayUsingPredicate:pred];
        return [filteredArr count];
    }
    

    要显示数据,您必须在 cellForRowAtIndexPathMethod 中执行与上述类似的操作:

    -(UITableViewCell *)tableView:(UITableView *)tableView
        cellForRowAtIndexPath:(NSIndexPath *)indexPath
    {
       static NSString *CellIdentifier =@"shopItems";
       UITableViewCell *cell = [tableView
                             dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
    
        //Get your section name from distinct array
        NSString *s = self.sectionData[indexPath.section]
        //Filter your main array
        NSPredicate *pred = [NSPredicate predicateWithFormat:@"shopIGroup like %@", s];
        NSArray *filteredArr = [self.shopArray filteredArrayUsingPredicate:pred];
       shopObjects *rightS = filteredArr[indexPath.row]
        cell.textLabel.text=rightS.shopITitle;
        cell.textLabel.backgroundColor=[UIColor clearColor];
     return cell;
    }
    

    我建议您更改数据结构。在这种项目中,我使用数组(mainArray)和另一个嵌入的数组(sectionArray),我在其中保存对象。所以我知道,例如我的第一个数组包含第一部分的另一个对象数组。 我可以是这样的部分:

    [mainArray count]
    

    每个部分的行数如下:

     mainArray[section].count
    

    以及每一行的数据:

    mainArray[section][row]
    

    希望你觉得它有用。

    【讨论】:

    • 感谢您的帮助 greg - 只是在努力理解 shopObjects *s = self.sectionData[section] 行 - 代码错误说部分未定义?我需要将“部分”添加为数组类型吗?还有这行 - ' NSArray *filteredArr = [self.shopArray filteredArrayUsingPredicate:pred];' - pred 未被识别..
    • 抱歉,将 [section] 替换为 [indexPath.section]。它应该完成这项工作。您不添加部分,而是从数组中读取对象,这与 [array objectAtIndex:section] 相同 - 您正在从索引(部分)获取对象。确保你有这一行 NSPredicate *pred = [NSPredicate predicateWithFormat:@"shopIGroup like %@", s.shopIGroup];上面,这是您创建 pred 的行。在我的帖子里。
    • 酷 - 有道理 - 为您的帮助欢​​呼 - 在我运行之前一切看起来都很好 - 打开 tableviewpage 时出现此错误 - 2013-12-03 17:06:43.958 GetPTFit[2800:70b] *** 由于未捕获的异常“NSInvalidArgumentException”而终止应用程序,原因:“-[__NSCFConstantString shopIGroup]:无法识别的选择器发送到实例 0x1c7c00”
    • 如何在 shopObjects 对象中声明 shopIGroup?你知道你从哪里得到错误(哪一行)?你能发布 shopObjects 类吗?
    • 嗨 Greg - 我已经在上面发布了 myshopObjects.M 文件 - 不知道如何找出是哪一行触发了错误?
    【解决方案2】:

    您可以使用来自TLIndexPathToolsTLIndexPathDataModel 而不是数组。它可以将您的数据组织成部分,并提供一堆方便的 API 来实现您的数据源和委托方法,例如 [dataModel numberOfRowsInSection:][dataModel itemForRowAtIndexPath:]

    在你的情况下,你会像这样创建你的数据模型:

    NAArray *items = ...;// array of shopObjects
    TLIndexPathDataModel *dataModel = [[TLIndexPathDataModel alloc] initWithItems:items sectionNameKeyPath:@"shopIGroup" identifierKeyPath:nil];
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-01-14
      • 2011-01-21
      • 1970-01-01
      • 2014-04-16
      • 1970-01-01
      • 1970-01-01
      • 2020-12-13
      • 2017-08-08
      相关资源
      最近更新 更多