objective-c - TableView 被委托(delegate),填充但不会分组

标签 objective-c ios uitableview uinavigationcontroller uitabbar

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

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

#import <UIKit/UIKit.h>

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

@property (nonatomic, retain) NSMutableArray *tableData;

-(void)initTableData;

@end

这是标题,如您所见,它没有任何异常。以下代码在我刚刚发布的 header 的 .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;
}

因为我不知道还能做什么。 (也没有用) 同样,我将委托(delegate)和数据源设置为文件所有者。

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

最佳答案

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

RootViewController *rvc = [[RootViewController] alloc] initWithStyle: UITableViewStyleGrouped];

关于objective-c - TableView 被委托(delegate),填充但不会分组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10191381/

相关文章:

objective-c - 您的第二个 iOS 应用程序 - 启用持久主列表

objective-c - 如何从协议(protocol)方法描述列表中解密 "objc_method_description"?

ios - 删除 iOS UIBarButtonItem 的标题文本

iOS 如何在 tableView 中找到侧滚动条的屏幕位置

ios - 在 SpriteKit 中创建可滚动表格的正确方法是什么?

objective-c - 循环遍历 NSStringEncoding(enum) 值

objective-c - 学习NSBlockOperation

ios - 该 bundle 无效。 UIRequiredDeviceCapability 中提供了未知的设备功能值

ios - 如何在 UITableViewCell 中向左或向右对齐 UILabel

swift - 将数据从 UIViewController 发送到 UIView