iphone - TableView 中的更多单元格

标签 iphone objective-c ios

我有一个 tableview,我正在用 JSON 填充它,但它使我拥有的值增加了一倍。我只有 2 个对象,但它在 tableview 中放入了 4 个对象。有人能告诉我我做错了什么以及为什么我在只有 2 个对象的情况下列出 4 个对象吗?我检查了计数和数据模型,它们都只显示 2 项。

这是我的代码:

    //
//  RootBeerTableViewController.m
//  BaseApp
//
//  Created by Blaine Anderson on 9/27/12.
//  Copyright (c) 2012 UIEvolution, Inc. All rights reserved.
//

#import "RootBeerTableViewController.h"
#import "GlobalData.h"

@interface RootBeerTableViewController ()



@end

@implementation RootBeerTableViewController

@synthesize rootList;

- (id)initWithStyle:(UITableViewStyle)style
{
    self = [super initWithStyle:style];
    if (self) {

    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];

   UITableView *tableView = [[UITableView alloc] initWithFrame:[[UIScreen mainScreen] applicationFrame] style:UITableViewStylePlain];

    tableView.autoresizingMask = UIViewAutoresizingFlexibleHeight|UIViewAutoresizingFlexibleWidth;

    tableView.delegate = self;

    tableView.dataSource = self;

    [tableView reloadData];



    self.view = tableView;
}

- (void)viewDidUnload
{
    [super viewDidUnload];
    // Release any retained subviews of the main view.
    // e.g. self.myOutlet = nil;
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}

#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    rootList =[[GlobalData sharedData].mRootBeers getRootBeers];
     NSLog(@"RootList: %@", rootList);
    // Return the number of sections.
    NSLog(@"RootList count: %i", rootList.count);
    return rootList.count;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
        // Return the number of sections.
     NSLog(@"RootList row count: %i", rootList.count);
    return rootList.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    rootList =[[GlobalData sharedData].mRootBeers rootBeerList];
     NSLog(@"RootList Cell: %@", rootList);
    RootBeers* mRootBeer = [rootList objectAtIndex:indexPath.row];


    static NSString *CellIdentifier = @"Cell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];

    if(cell == nil){
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle  reuseIdentifier:@"Cell"]; 
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
    }
    NSLog(@"Cell Name: %@", mRootBeer.name);
       // Configure the cell...
    cell.textLabel.text = mRootBeer.name; 
    cell.detailTextLabel.text = mRootBeer.brewer; 



    return cell;
}

/*
// Override to support conditional editing of the table view.
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath
{
    // Return NO if you do not want the specified item to be editable.
    return YES;
}
*/

/*
// Override to support editing the table view.
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (editingStyle == UITableViewCellEditingStyleDelete) {
        // Delete the row from the data source
        [tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];
    }   
    else if (editingStyle == UITableViewCellEditingStyleInsert) {
        // Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view
    }   
}
*/

/*
// Override to support rearranging the table view.
- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)fromIndexPath toIndexPath:(NSIndexPath *)toIndexPath
{
}
*/

/*
// Override to support conditional rearranging of the table view.
- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath
{
    // Return NO if you do not want the item to be re-orderable.
    return YES;
}
*/

#pragma mark - Table view delegate

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{

    [[GlobalData sharedData].mViewManager pushView:DETAILVIEW animated:YES]; 



}

@end

最佳答案

我相信这就是原因......

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
rootList =[[GlobalData sharedData].mRootBeers getRootBeers];
 NSLog(@"RootList: %@", rootList);
// Return the number of sections.
NSLog(@"RootList count: %i", rootList.count);
return rootList.count;
}

改成这个。

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return 1;
}

您将为 rootList 中的每个对象添加一个部分,然后在每个部分中添加所有 rootlist。因此,仅返回一个部分是最简单的。

关于iphone - TableView 中的更多单元格,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12665166/

相关文章:

iphone - 在 canEditRowAtIndexPath 方法中,UITableView 的 reloadData 不能正常工作?

iphone - UIToolbar 和其他 View

objective-c - 仅在第一次运行 iOS 时执行功能

ios - UIView 可以复制吗?

objective-c - 如何全局更改UIKit属性的默认值?

iphone - 为什么 view2 没有出现在这段代码中? (将 UIView 2 合并到 UIView 1 中)

iphone - RestKit ObjectLoader 无法识别的选择器试图映射对象

ios - 签名无效 - Itunes Connect

iphone - 如何通过短信发送网址?

iphone - 在 iPhone 上使用 CoreData 或 SQLite?