0

我有一个奇怪的问题,我找不到解决方案(或类似的东西)。问题是,我的 UITableView 填充了初始信息(用于测试),但无论我做什么,我似乎都无法将其设置为分组样式(我可以在 UI 上选择它,但它不会显示)

我最初启动了一个 TabBar 项目,并在选项卡中添加了第三个 navigationController 视图。

#import <UIKit/UIKit.h>

@interface RootViewController : UITableViewController <UITableViewDelegate, UITableViewDataSource> {
NSMutableArray *tableData;
}

@property (nonatomic, retain) NSMutableArray *tableData;

-(void)initTableData;

@end

这是标题,如您所见,它没有任何异常。以下代码位于我刚刚发布的标头的 .m 文件中(只会发布未注释的代码:

@synthesize tableData;

-(void)initTableData
{
    tableData = [[NSMutableArray alloc] init];
    [tableData addObject:@"Cidade"];
    [tableData addObject:@"Veículo"];
    [tableData addObject:@"Ano"];
    [tableData addObject:@"Valor"];
    [tableData addObject:@"Cor"];
    [tableData addObject:@"Combustível"];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"Busca";
    UIBarButtonItem *_backButton = [[UIBarButtonItem alloc] initWithTitle:@"Back" style:UIBarButtonItemStyleDone target:nil action:nil];
    self.navigationItem.backBarButtonItem = _backButton;
    [self initTableData];
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    // Return the number of sections.
    return 1;
}


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    // Return the number of rows in the section.
    return 6;
}


// Customize the appearance of table view cells.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

    static NSString *CellIdentifier = @"Cell";

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
    }

    // Configure the cell...
    cell.textLabel.text = [tableData objectAtIndex:[indexPath row]];

    return cell;
}

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

如您所见,没有什么不寻常的事了……知道可能是什么原因造成的吗?我试过了

- (id)initWithStyle:(UITableViewStyle)style {
    // Override initWithStyle: if you create the controller programmatically and want to perform customization that is not appropriate for viewDidLoad.
    self = [super initWithStyle:UITableViewStyleGrouped];
    if (self) {
        // Custom initialization.
    }
    return self;
}

因为我不知道还能做什么。(也没有工作)再次,我将委托和数据源设置为文件的所有者。

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {    

    // Override point for customization after application launch.

    // Set the tab bar controller as the window's root view controller and display.

    self.window.rootViewController = self.tabBarController;
    RootViewController *rvc = [[RootViewController alloc] initWithStyle: UITableViewStyleGrouped];

    [self.window makeKeyAndVisible];

    return YES;
}
4

2 回答 2

1

分组表响应这些部分。您只列出了 1 个部分,因此您只会看到 1 个组。尝试为第二组添加第二个 tableData 并返回 2 个部分。您还必须按部分拆分 -cellForRowAtIndexPath 中的数据,以确保数据进入正确的部分。

if (indexpath.section == 0) {
   // first section and first tableData
}
if (indexpath.section == 1) {
   // second section and second tableData
}
于 2012-04-17T12:56:03.147 回答
1

由于您已修改- (id)initWithStyle:(UITableViewStyle)style初始化程序以返回UITableView具有分组样式的 a,因此在初始化 ? 时是否调用此初始化程序RootViewController

RootViewController *rvc = [[RootViewController] alloc] initWithStyle: UITableViewStyleGrouped];
于 2012-04-17T12:50:30.427 回答