ios - UiTableView-将内容添加到相关部分

标签 ios objective-c uitableview

我首先要说我是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;
}


我的问题是-


如何计算上述方法中return语句所需的每个节中的标头数和行数?
然后如何将单元格应用到相关部分?


更新
我的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等等[3321:70b]你好值= 0
    2013-12-05 10:16:18.596等等[3321:70b]你好值= 1
    2013-12-05 10:16:18.596等等[3321:70b]你好值= 2
    2013-12-05 10:16:27.962等等[3321:70b]节:3
    2013-12-05 10:16:27.962等等[3321:70b]-[NSCFConstantString shopIGroup]:无法识别的选择器已发送到实例0x1c7c38
    2013-12-05 10:16:27.965等等[3321:70b] ***由于未捕获的异常'NSInvalidArgumentException'终止了应用程序,原因:'-[__ NSCFConstantString shopIGroup]:无法识别的选择器已发送到实例0x1c7c38'
    ***首先抛出调用堆栈:
    (
    0 CoreFoundation 0x02bde5e4 __exceptionPreprocess + 180
    1 libobjc.A.dylib 0x029618b6 objc_exception_throw + 44
    2 CoreFoundation 0x02c7b903-[NSObject(NSObject)didNotRecognizeSelector:] + 275
    3 CoreFoundation 0x02bce90b ___forwarding_ + 1019
    4 CoreFoundation 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 layoutSubviews] + 213
    19 UIKit 0x0084c267-[UIView(CALayerDelegate)layoutSublayersOfLayer:] + 355
    20 libobjc.A.dylib 0x0297381f-[NSObject performSelector:withObject:] + 70
    21 QuartzCore 0x0221a2ea-[CALayer layoutSublayers] + 148
    22 QuartzCore 0x0220e0d4 _ZN2CA5Layer16layout_if_neededEPNS_11TransactionE + 380
    23 QuartzCore 0x0220df40 _ZN2CA5Layer28layout_and_display_if_neededEPNS_11TransactionE + 26
    24 QuartzCore 0x02175ae6 _ZN2CA7Context18commit_transactionEPNS_11TransactionE + 294
    25 QuartzCore 0x02176e71 _ZN2CA11Transaction6commitEv + 393
    26 QuartzCore 0x02177544 _ZN2CA11Transaction17observer_callbackEP19__CFRunLoopObservermPv + 92
    27 CoreFoundation 0x02ba64ce __CFRUNLOOP_IS_CALLING_OUT_TO_AN_OBSERVER_CALLBACK_FUNCTION + 30
    28 CoreFoundation 0x02ba641f __CFRunLoopDoObservers + 399
    29 CoreFoundation 0x02b84344 __CFRunLoopRun + 1076
    30 CoreFoundation 0x02b83ac3 CFRunLoopRunSpecific + 467
    31 CoreFoundation 0x02b838db CFRunLoopRunInMode + 123
    32个GraphicsServices 0x03c349e2 GSEventRunModal + 192
    33 GraphicsServices 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) 


错误画面-

最佳答案

您可以通过从数组中获取对象的不同部分来获取部分。最好的方法是添加新的@Array属性(例如,稍后将需要使用)NSArray类型,并在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;
}


我建议您更改数据结构。在这种项目中,我将array(mainArray)与另一个保留对象的数组(sectionArray)结合使用。因此,我知道例如我的第一个数组包含用于第一部分的另一个对象数组。
我可以是这样的部分:

[mainArray count]


每节的行是这样的:

 mainArray[section].count


以及每一行的数据:

mainArray[section][row]


希望你觉得它有用。

关于ios - UiTableView-将内容添加到相关部分,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20355593/

相关文章:

ios - Swift 4 在 UITableView 中显示来自 api 的图像的正确方法

ios - 如何通过搜索栏创建到另一个 View Controller 的转场?

ios - 在 Objective C UITableVIew 中如何在调用重新加载后保留滚动位置

ios - 在 GameScene.swift 中更改 GameViewController.swift 对象的属性

ios - 从 UIImage 获取 CurrentBackgroundImage 路径

iphone - 滚动 - 从图像创建缩略图

objective-c - Swift桥接头导入导入Hopscotch-Swift.h本身的文件时如何防止循环引用

ios - 通过 observeSingleEventOfType 将 Firebase 日期加载到 UITableView 并附加 ChildAdded Observer

iphone - (iPhone/iOS) UISearchBar 范围按钮覆盖 UITableView 的第一行

ios - 辅助功能检查器不适用于 iOS 11.2