objectivec-将另一行添加到tableView iOS



这是我第一次问,希望你能帮助我!好吧,问题是我现在正在做Big Nerd Ranch iOS指南,我有一个挑战,我必须用两个部分填充表格视图,一个部分用于50美元以上的项目,另一个部分则用于50美元以下的项目。在所有的项目列表之后,我必须添加一个单元格,指定没有更多的项目要添加。假设我有5个项目,2个在第一节,3个在另一节。现在,我将发布代码,以便您可以查看。我发现的问题是罗的数量。我想是一节,当然是没有更多的项目!出现在最后一个项目的末尾。希望你能帮助我!

@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

试试这个:

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

希望这能有所帮助…:)

为此,我想您不必在每个部分的末尾创建一个新的单元格,说明不再添加任何项目。相反,你可以使用

tableView:titleForFooterInSection:
tableView:viewForFooterInSection:
tableView:heightForFooterInSection:

方法,并将自定义页脚呈现为您的部分,以使其看起来像您的表单元格。

最新更新