这是我第一次询问,希望你能帮助我!好吧,问题是我现在正在做Big Nerd Ranch iOS指南,我有一个挑战,我必须填写两个部分的tableview,1个超过50美元的项目,和其他50美元以下的项目。在所有项目列表之后,我必须添加一个单元格,指定不再需要添加的项目。假设我有5个项目,第一部分为2,另一部分为3。现在,我将发布代码,以便您查看它。我发现的问题是我猜的numberofRowsInSection,当然没有更多的项目!在最后一项结束后出现。希望你能帮助我!
@interface JDMItemsViewController()
@property (nonatomic) NSMutableArray *moreThanFifty;
@property (nonatomic) NSMutableArray *lessThanFifty;
@property (nonatomic) NSArray *rowsForSections;
@property (nonatomic) NSArray *items;
@end
@implementation JDMItemsViewController
-(instancetype)init
{
//Call the superclass's designated initializer
self = [super initWithStyle:UITableViewStylePlain];
if (self) {
for (int i = 0; i < 5; i++) {
[[JDMItemStore sharedStore] createItem];
}
}
return self;
}
-(instancetype)initWithStyle:(UITableViewStyle)style
{
return [self init];
}
#pragma mark - tableview datasource and delegate methods
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
if ([self.items count] > 0) {
return [self.rowsForSections count];
}else {
return 1;
}
}
-(NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{
NSArray *headers = [NSArray array];
if ([self.items count] == 0) {
headers = @[@"No items to show"];
return [headers objectAtIndex:0];
}else {
headers = @[@"Items with value above 50",@"Items with value under 50"];
return [headers objectAtIndex:section];
}
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
if ([self.items count] > 0) {
if (section == 0) {
self.moreThanFifty = [self overFifty:[[JDMItemStore sharedStore]allItems]];
return [self.moreThanFifty count] + 1;
}
if (section == 1) {
self.lessThanFifty = [self underFifty:[[JDMItemStore sharedStore]allItems]];
return [self.lessThanFifty count] + 1;
}
}
return 1;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
// UITableViewCell *cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"UITableViewCell"];
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"UITableViewCell" forIndexPath:indexPath];
//NSArray *items = [[JDMItemStore sharedStore] allItems];
NSInteger lastSectionIndex = [tableView numberOfSections];
NSInteger lastRowIndex = [tableView numberOfRowsInSection:lastSectionIndex] - 1;
__unused NSIndexPath *pathToLastRow = [NSIndexPath indexPathForRow:lastRowIndex inSection:lastSectionIndex];
if ([self.items count] > 0) {
if (indexPath.section == 0) {
[self overFifty:self.items];
if (indexPath.row < [self.moreThanFifty count]) {
cell.textLabel.text = [self.moreThanFifty[indexPath.row] description];
}else {
cell.textLabel.text = @"No more Items!";
}
}
if (indexPath.section == 1) {
[self underFifty:self.items];
if (indexPath.row < [self.lessThanFifty count]) {
cell.textLabel.text = [self.lessThanFifty[indexPath.row] description];
}else {
cell.textLabel.text = @"No more Items!";
}
}
}
else if([self.items count] == 0) {
cell.textLabel.text = @"No more Items!";
}
return cell;
}
-(void)viewDidLoad
{
[super viewDidLoad];
self.items = [[JDMItemStore sharedStore] allItems];
self.moreThanFifty = [NSMutableArray array];
self.lessThanFifty = [NSMutableArray array];
self.rowsForSections = @[self.moreThanFifty,self.lessThanFifty];
[self.tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:@"UITableViewCell"];
}
#pragma mark - helper methods
-(NSMutableArray *)overFifty:(NSArray *)items
{
if ([items count] > 0) {
for (JDMItem * i in items) {
if (i.valueInDollars >= 50) {
[self.moreThanFifty addObject:i];
}
}
}
return self.moreThanFifty;
}
-(NSMutableArray *)underFifty:(NSArray *)items
{
if ([items count] > 0) {
for (JDMItem *i in items) {
if (i.valueInDollars < 50) {
[self.lessThanFifty addObject:i];
}
}
}
return self.lessThanFifty;
}
@end
答案 0 :(得分:0)
试试这个:
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
if ([self.items count] > 0) {
return 2;
}else {
return 1;
}
}
-(NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{
NSArray *headers = [NSArray array];
if ([self.items count] == 0) {
headers = @[@"No items to show"];
return [headers objectAtIndex:0];
}else {
headers = @[@"Items with value above 50",@"Items with value under 50"];
return [headers objectAtIndex:section];
}
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
if ([self.items count] > 0) {
if (section == 0) {
self.moreThanFifty = [self overFifty:[[JDMItemStore sharedStore]allItems]];
return [self.moreThanFifty count] + 1;
}else if (section == 1) {
self.lessThanFifty = [self underFifty:[[JDMItemStore sharedStore]allItems]];
return [self.lessThanFifty count] + 1;
}
}
return 0;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
// UITableViewCell *cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"UITableViewCell"];
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"UITableViewCell" forIndexPath:indexPath];
//NSArray *items = [[JDMItemStore sharedStore] allItems];
NSInteger lastSectionIndex = [tableView numberOfSections];
NSInteger lastRowIndex = [tableView numberOfRowsInSection:lastSectionIndex] - 1;
__unused NSIndexPath *pathToLastRow = [NSIndexPath indexPathForRow:lastRowIndex inSection:lastSectionIndex];
if ([self.items count] > 0) {
if (indexPath.section == 0) {
//[self overFifty:self.items];
if (indexPath.row < [self.moreThanFifty count]) {
cell.textLabel.text = [self.moreThanFifty[indexPath.row] description];
}
else{
cell.textLabel.text = @"No more Items!";
}
}
else if (indexPath.section == 1) {
if (indexPath.row < [self.lessThanFifty count]) {
cell.textLabel.text = [self.lessThanFifty[indexPath.row] description];
}
else{
cell.textLabel.text = @"No more Items!";
}
}
}
return cell;
}
希望这会有所帮助.. :)
答案 1 :(得分:0)
我想为此你不必在每个部分的末尾创建一个新的单元格,说明不再添加任何项目。相反,你可以使用
tableView:titleForFooterInSection:
tableView:viewForFooterInSection:
tableView:heightForFooterInSection:
方法并将自定义页脚呈现为模仿您的表格单元格的部分。