ios - 计算类的对象数

标签 ios objective-c iphone ios7

我已经声明了一个 Recipe 类并手动创建了 16 个对象。在我的应用程序中,一个对象就是我的一行。

所以我需要在方法 numberOfRowsInSection 中返回行数

如何返回 Recipe 类拥有的对象数量

这是我的view controller.m文件

- (void)viewDidLoad
{
    [super viewDidLoad];

    Recipe *recipe1 = [Recipe new];
    recipe1.name = @"Egg Benedict";
    recipe1.prepTime = @"30 min";
    recipe1.imageFile = @"egg_benedict.jpg";
    recipe1.ingredients = [NSArray arrayWithObjects:@"2 fresh English muffins", @"4 eggs", @"4 rashers of back bacon", @"2 egg yolks", @"1 tbsp of lemon juice", @"125 g of butter", @"salt and pepper", nil];

    Recipe *recipe2 = [Recipe new];
    recipe2.name = @"Mushroom Risotto";
    recipe2.prepTime = @"30 min";
    recipe2.imageFile = @"mushroom_risotto.jpg";
    recipe2.ingredients = [NSArray arrayWithObjects:@"1 tbsp dried porcini mushrooms", @"2 tbsp olive oil", @"1 onion, chopped", @"2 garlic cloves", @"350g/12oz arborio rice", @"1.2 litres/2 pints hot vegetable stock", @"salt and pepper", @"25g/1oz butter", nil];

    Recipe *recipe3 = [Recipe new];
    recipe3.name = @"Full Breakfast";
    recipe3.prepTime = @"20 min";
    recipe3.imageFile = @"full_breakfast.jpg";
    recipe3.ingredients = [NSArray arrayWithObjects:@"2 sausages", @"100 grams of mushrooms", @"2 rashers of bacon", @"2 eggs", @"150 grams of baked beans", @"Vegetable oil", nil];
    .
    .
    .
   // up to 16 objects
    .
    .
    .
    Recipe *recipe15 = [Recipe new];
    recipe15.name = @"Angry Birds Cake";
    recipe15.prepTime = @"4 hours";
    recipe15.imageFile = @"angry_birds_cake.jpg";
    recipe15.ingredients = [NSArray arrayWithObjects:@"12 tablespoons (1 1/2 sticks) unsalted butter", @"2 1/2 cups all-purpose flour", @"1 tablespoon baking powder", @"1 teaspoon salt", @"1 3/4 cups sugar", @"2 large eggs, plus 3 large egg yolks", @"1 cup of milk", nil];

    Recipe *recipe16 = [Recipe new];
    recipe16.name = @"Ham and Cheese Panini";
    recipe16.prepTime = @"10 min";
    recipe16.imageFile = @"ham_and_cheese_panini.jpg";
    recipe16.ingredients = [NSArray arrayWithObjects:@"2 tablespoons unsalted butter", @"4 cups thinly sliced shallots", @"2 teaspoons fresh thyme", @"1/4 cup grainy Dijon mustard", @"8 slices rustic white bread", @"8 slices Gruyere cheese", @"8 ounces sliced cooked ham", nil];

}

这是我的 numberOfRowInSection 方法

如何返回行数,即 16

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    //how can I return no of rows (no of objects)

}

最佳答案

显而易见的解决方案是使用数组,将所有食谱放入一个数组中,然后在 tableView 数据源上返回此类数组的计数。有点像

// Declare it on your interface
@property (nonatomic, strong) NSArray *recipes;
...

// On your viewDidLoad
self.recipes = @[recipe1, recipe2]; // and so on
...

// UITableViewDataSource
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.recipes.count;
}

关于ios - 计算类的对象数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28514031/

相关文章:

ios - Xcode iPhone SDK - 上传文件

ios - 单击按钮时将数据存储在 NSMutable 数组中

iphone - 如何在 iOs 中使用 CLGeoCoder 使用邮政编码/邮政编码获取经纬度?

iphone - 奇怪的 UIView 坐标问题

iphone - 在 Storyboard 中编辑自定义 UIView 子类

ios - 取消 AFNetworking 2.0 成功 block

ios - Xcode 项目组结构的最佳实践?

ios - Swift3 Xcode 8 : 'none' is unavailable: use [] to construct an empty option set ; What should I do?

objective-c - Objective c NSArray 的元素最优雅的语法是什么(相当于 Python 的)?

iphone - 如何防止由 NSFetchedResultsChangeInsert 和 insertRowsAtIndexPaths 插入的屏幕外单元格调用 cellForRowAtIndexPath