UITableView section 分段显示

iOS 编程(第四版)第八章初级练习习题。


//

//BNRItemsViewController.m

//Homepwner

//

//Created by David on 5/25/16.

//Copyright © 2016 David. All rights reserved.

//

#import"BNRItemsViewController.h"

#import"BNRItemStore.h"

#import"BNRItem.h"

@interfaceBNRItemsViewController()

@propertyNSMutableArray*arr1;

@propertyNSMutableArray*arr2;

@end

@implementationBNRItemsViewController

-(instancetype)init

{

self= [superinitWithStyle:UITableViewStylePlain];

if(self) {

for(inti =0; i <5; i++) {

[[BNRItemStoresharedStore]createItem];

}

}

returnself;

}

-(instancetype)initWithStyle:(UITableViewStyle)style

{

return[selfinit];

}

- (NSInteger)numberOfSectionsInTableView:(UITableView*)tableView

{

return2;

}

- (NSString*)tableView:(UITableView*)tableView titleForHeaderInSection:(NSInteger)section

{

if(section ==0) {

return@"More than 50";

}elseif(section ==1) {

return@"Less and equal to 50";

}else{

returnnil;

}

}

- (NSInteger)tableView:(UITableView*)tableView

numberOfRowsInSection:(NSInteger)section

{

NSArray*all = [[BNRItemStoresharedStore]allItems];

self.arr1= [[NSMutableArrayalloc]init];

self.arr2= [[NSMutableArrayalloc]init];

for(BNRItem*iteminall) {

if(item.valueInDollars>50) {

[self.arr1addObject:item];

}else{

[self.arr2addObject:item];

}

}

if(section ==0) {

return[self.arr1count];

}else{

return[self.arr2count];

}

}

- (UITableViewCell*)tableView:(UITableView*)tableView

cellForRowAtIndexPath:(NSIndexPath*)indexPath

{

UITableViewCell*cell =

[tableViewdequeueReusableCellWithIdentifier:@"UITableViewCell"forIndexPath:indexPath];

NSUIntegerrow = indexPath.row;

NSUIntegersection = indexPath.section;

if(section ==0) {

BNRItem*item =self.arr1[row];

cell.textLabel.text= [itemdescription];

returncell;

}

if(section ==1) {

BNRItem*item = [self.arr2objectAtIndex:row];

cell.textLabel.text= [itemdescription];

returncell;

}

returnnil;

}

- (void)viewDidLoad

{

[superviewDidLoad];

[self.tableViewregisterClass:[UITableViewCellclass]forCellReuseIdentifier:@"UITableViewCell"];

}

@end

你可能感兴趣的:(UITableView section 分段显示)