【问题标题】:trouble with making the style of the tableview制作表格视图的样式时遇到问题
【发布时间】:2011-12-05 17:33:19
【问题描述】:

我开发标签栏应用程序,每个标签视图都是一个表格视图。所以我的问题是,在第一个选项卡中我开发了一个普通的表格视图,但在第二个选项卡中我需要一个分组的。我在 xib 中将样式更改为分组,并在方法中这样做:

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

但模拟器仍然向我显示一个简单的表格视图。当我尝试在新应用程序中编写相同的代码时 - 一切正常。谁能帮忙解释一下为什么它不起作用?

整个代码:

- (void)viewDidLoad
{
    [super viewDidLoad];

    NSArray *row1 = [[NSArray alloc] initWithObjects:@"aaa1", @"aaa2", @"aaa3", nil];
    NSArray *row2 = [[NSArray alloc] initWithObjects:@"bbb1", @"bbb2", @"bbb3", nil];
    NSArray *row3 = [[NSArray alloc] initWithObjects:@"ccc1", @"ccc2", @"ccc3", nil];
    NSDictionary *dict = [[NSDictionary alloc] initWithObjectsAndKeys:
                          row1, @"a",
                          row2, @"b",
                          row3, @"c", nil];
    self.list = dict;


    NSArray *array = [[list allKeys] sortedArrayUsingSelector:@selector(compare:)];
    self.keys = array;
    [row1 release];
    [row2 release];
    [row3 release];
    [dict release];
}


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    NSString *key = [keys objectAtIndex:section]; 
    NSArray *nameSection = [list objectForKey:key];
    return [nameSection count];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSUInteger section = [indexPath section];
    NSUInteger row = [indexPath row];

    NSString *key = [self.keys objectAtIndex:section]; 
    NSArray *nameSection = [self.list objectForKey:key];

    static NSString *CellIdentifier = @"Cell";

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

    cell.textLabel.text = [nameSection objectAtIndex:row];

    return cell;
}

-(NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section {
    NSString *key = [self.keys objectAtIndex:section]; 
    return key;
}

实际上没什么,我在 XIB 文件中的属性检查器中进行的更改似乎有效。谁能帮忙?

【问题讨论】:

  • 你实现了 UITableViewDelegate 和 UITableViewDataSource 方法了吗?并且您是否有大于 1 的部分数量才能显示为分组?请提供更多代码。 if(self) 的意义何在?
  • if (self) is a provided code when selection that new view controller shall be a table view controller

标签: ios uitableview


【解决方案1】:

确保您的班级是UITableViewController,因为您正在使用:

- (id)initWithStyle:(UITableViewStyle)style

示例

@interface MyViewController : UITableViewController
{
    NSString * something;
}

@end

或者

如果这不是您的 rootViewController,您始终可以确保表格按以下方式分组:

MyViewController * mvc = [[MyViewController alloc] initWithStyle:UITableViewStyleGrouped];

【讨论】:

  • 我检查过 - 我的课程是 UITableViewController
  • 后者到底应该放在哪个方法中?
猜你喜欢
  • 2020-04-08
  • 2010-12-23
  • 2019-08-19
  • 1970-01-01
  • 2013-05-02
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多