对象的numberOfRowsInSection

时间:2014-05-27 00:27:01

标签: ios nsarray

我有一个我正在编写的代码用于测试。

在它上面我将创建对象实例,其中的名称将是行。

示例:名称Lucille:将一起展示“Frango”和“Beef Brisket”;

我无法弄清楚如何做三件事:

  1. 按字母顺序整理行;
  2. 每行显示正确的单元格数量;
  3. 仅显示它们所属行的相应单元格(名称为类别);
  4. 这是我到目前为止所做的:

    #import "EAMasterViewController.h"
    
    #import "EADetailViewController.h"
    #import "review.h"
    #import "CelulaTableViewCell.h"
    
    @interface EAMasterViewController () {
        NSMutableArray *_objects;
        NSMutableArray *secoes;
        NSMutableArray *dicrows;
        NSMutableDictionary *nn;
    
    }
    @end
    
    @implementation EAMasterViewController
    
    - (void)awakeFromNib
    {
        [super awakeFromNib];
    }
    
    - (void)viewDidLoad
    {
        [super viewDidLoad];
        // Do any additional setup after loading the view, typically from a nib.
        self.navigationItem.leftBarButtonItem = self.editButtonItem;
    
        //Bar "+"  Button
        UIBarButtonItem *addButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAdd target:self action:@selector(insertNewObject:)];
        self.navigationItem.rightBarButtonItem = addButton;
    
    
        _tabela = [NSMutableArray array];
        [_tabela addObject:[review newReviewWithName:@"Luccille" dish:@"Frango" rating:@"3"]];
        [_tabela addObject:[review newReviewWithName:@"In-n-Out" dish:@"Camarão grelhado" rating:@"5"]];
        [_tabela addObject:[review newReviewWithName:@"Azul Tequila" dish:@"Frango grelhado" rating:@"1"]];
        [_tabela addObject:[review newReviewWithName:@"Charcoal" dish:@"Cheese Burguer" rating:@"4"]];
        [_tabela addObject:[review newReviewWithName:@"Luccille" dish:@"Beef Brisket" rating:@"2"]];
        [_tabela addObject:[review newReviewWithName:@"In-n-Out" dish:@"double-double" rating:@"1"]];
        [_tabela addObject:[review newReviewWithName:@"Azul Tequila" dish:@"Chips" rating:@"5"]];
        [_tabela addObject:[review newReviewWithName:@"Azul Tequila" dish:@"Dips" rating:@"4"]];
        [_tabela addObject:[review newReviewWithName:@"Mooses" dish:@"Dips" rating:@"4"]];
        [_tabela addObject:[review newReviewWithName:@"In-n-Out" dish:@"Cheese Burguer" rating:@"5"]];
    
    
    
        self.arraySections;
    
    }
    
    
    - (void)insertNewObject:(id)sender
    {
        if (!_objects) {
            _objects = [[NSMutableArray alloc] init];
        }
        [_objects insertObject:[NSDate date] atIndex:0];
        NSIndexPath *indexPath = [NSIndexPath indexPathForRow:0 inSection:0];
        [self.tableView insertRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationAutomatic];
    }
    
    #pragma mark - Table View
    
    - (void) arraySections{
        secoes = [[NSMutableArray alloc]init];
    
        int c, count = 0, d;
    
        for(c=0;c<_tabela.count;c++){
    
            for(d=0;d<count;d++)
            {
                review *temp1 = _tabela[(c)];
    
                if ([temp1.restaurant isEqualToString:secoes[(d)]])
                   break;
            }
            if (d == count)
            {
                review *temp = _tabela[(c)];
                secoes[(count)] = temp.restaurant;
                count++;
            }
        }
    
    
    
        ///  **** Nuber of rows in Section
    
        dicrows = [[NSMutableArray alloc]init];
        nn = [[NSMutableDictionary alloc]init];
    
        NSMutableArray *rows = [[NSMutableArray alloc]initWithArray:secoes];
    
        NSLog(@"%@",rows);
    
        for (int i = 0; i<rows.count; i++) {
            NSString *temp = rows[(i)];
            NSInteger x = 0;
            for (int j =0; j<_tabela.count; j++) {
                review *temp1 = _tabela[(j)];
                //            NSLog(@"tabela rest em J é: %@",temp1.restaurant);
                if ([temp isEqualToString:temp1.restaurant]) {
                    x++;
                }
    
            }
    
            [dicrows addObject:[[NSMutableDictionary alloc]initWithObjectsAndKeys:temp, @"restaurant",
                                                                                [NSNumber numberWithInt:x], @"quantidade", nil]];
            [nn setObject:temp forKey:@"restaurant"];
            [nn setObject:[NSNumber numberWithInt:x] forKey:@"quantidade"];
        }
    
    }
    
    - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
    {
        return [secoes count];
    }
    
    - (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section {
            return [secoes objectAtIndex:section];
    }
    
    - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
    {
    
        NSLog(@"oi");
        return ??????????????;
    }
    
    - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
    {
    
        CelulaTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"Cell" forIndexPath:indexPath];
    
        review *reviews = _tabela[indexPath.row];
        cell.nome_restaurante.text = reviews.restaurant;
        cell.nome_rating.text = reviews.rating;
        cell.nome_dish.text = reviews.dish;
    
    
        return cell;
    }
    
    @end
    

    这是review.m

    #import "review.h"
    
    @implementation review
    
    
    
    +(review *)newReviewWithName:(NSString *)restaurant
                            dish:(NSString *)dish
                          rating:(NSString *)rating{
    
        review *Review = [[review alloc]init];
        Review.restaurant = restaurant;
        Review.dish = dish;
        Review.rating = rating;
        return Review;
    }
    
    
    @end
    

    我很感激任何帮助。我已经看过几个人们在行和节中使用NSDictionary的地方,但我真的想尝试使它在类审查中使用强制转换.m。

    由于

1 个答案:

答案 0 :(得分:0)

#import "ViewController.h"
#import "Review.h"

@interface ViewController () <UITableViewDelegate, UITableViewDataSource>
@property (nonatomic,  retain) NSMutableDictionary *dict;
@end

@implementation ViewController

- (NSMutableDictionary *)dict
{
    if(!_dict) {
        _dict = [[NSMutableDictionary alloc] init];
    }
    return _dict;
}

//- (void)dealloc
//{
//    [_dict release];
//    [super dealloc];
//}

- (void)addReview:(Review *)review
{
    if(review.restaurant) {

        NSMutableArray *arr = self.dict[review.restaurant];
        if(!arr) {
            arr = [NSMutableArray array];
            [self.dict setObject:arr forKey:review.restaurant];
        }

        [arr addObject:review];
    }
}

- (void)viewDidLoad
{
    [super viewDidLoad];

    NSMutableArray *arr = [NSMutableArray array];
    [arr addObject:[Review reviewWithRestaurant:@"Luccille" dish:@"Frango" rating:@"3"]];
    [arr addObject:[Review reviewWithRestaurant:@"In-n-Out" dish:@"Camarão grelhado" rating:@"5"]];
    [arr addObject:[Review reviewWithRestaurant:@"Azul Tequila" dish:@"Frango grelhado" rating:@"1"]];
    [arr addObject:[Review reviewWithRestaurant:@"Charcoal" dish:@"Cheese Burguer" rating:@"4"]];
    [arr addObject:[Review reviewWithRestaurant:@"Luccille" dish:@"Beef Brisket" rating:@"2"]];
    [arr addObject:[Review reviewWithRestaurant:@"In-n-Out" dish:@"double-double" rating:@"1"]];
    [arr addObject:[Review reviewWithRestaurant:@"Azul Tequila" dish:@"Chips" rating:@"5"]];
    [arr addObject:[Review reviewWithRestaurant:@"Azul Tequila" dish:@"Dips" rating:@"4"]];
    [arr addObject:[Review reviewWithRestaurant:@"Mooses" dish:@"Dips" rating:@"4"]];
    [arr addObject:[Review reviewWithRestaurant:@"In-n-Out" dish:@"Cheese Burguer" rating:@"5"]];

    [arr sortUsingComparator:^NSComparisonResult(id obj1, id obj2) {
        Review *review1 = (Review *)obj1;
        Review *review2 = (Review *)obj2;

        NSComparisonResult result = [review1.restaurant compare:review2.restaurant];
        if(result == NSOrderedSame) {
            result = [review1.dish compare:review2.dish];
        }
        return result;
    }];

    [arr enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) {
        [self addReview:obj];
    }];

}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return self.dict.allKeys.count;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    id key = self.dict.allKeys[section];
    NSArray *arr = self.dict[key];
    return arr.count;
}

- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{
    return self.dict.allKeys[section];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"Cell" forIndexPath:indexPath];
    if(!cell) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"Cell"];
//        [cell autorelease];
    }

    id key = self.dict.allKeys[indexPath.section];
    NSArray *arr = self.dict[key];
    Review *review = arr[indexPath.row];

    cell.textLabel.text = [NSString stringWithFormat:@"%@ / %@ / %@", review.restaurant, review.dish, review.rating];
    return cell;
}

@end

您可以使用此代码进行测试。有一点你应该知道,建议在类名的第一个字母处使用大写字母。 最后,此代码适用于ARC项目。如果您使用ARC项目,只需删除评论。