【问题标题】:Crashed With Implementing Multiple Sections In UITableView在 UITableView 中实现多个部分时崩溃
【发布时间】:2014-01-12 11:27:26
【问题描述】:

我现在正在学习有关 iOS 开发的 UITableView 课程。我想在我的 tableView 中实现三个部分,但是当我点击包含 tableView 的 tabBarItem 时它总是崩溃。我的代码如下:

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        self.title = @"Settings";
        self.tabBarItem.image = [UIImage imageNamed:@"19-gear.png"];
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    NSArray *list1 = [[NSMutableArray alloc] initWithObjects:@"fbfb", @"dfhbfh", @"rtjtj", @"rgrrth", @"grhh", @"tyjtyj", nil];
    NSArray *list2 = [[NSMutableArray alloc] initWithObjects:@"fgngg", @"yjyj", @"rhrht", @"rthfh", @"tjtyjyj", @"fsfsdf", nil];
    NSArray *list3 = [[NSMutableArray alloc] initWithObjects:@"24242", @"24242", @"24242", @"4242", @"4242", @"24242", nil];

    self.array1 = list1;
    self.array2 = list2;
    self.array3 = list3;

    [list1 release];
    [list2 release];
    [list3 release];
}

#pragma mark -
#pragma mark - TableView Data Source Methods

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

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    switch (section) {
        case 0:
            return [array1 count];
            break;
        case 1:
            return [array2 count];
            break;
        case 2:
            return [array3 count];
            break;
        default:
            break;
    }
    return 0;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *cells = @"Cells";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cells];
    if (cell == nil) 
    {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cells];
    }
    switch (indexPath.section) {
        case 0:
            cell.textLabel.text = [array1 objectAtIndex:indexPath.row];
            break;
        case 1:
            cell.textLabel.text = [array2 objectAtIndex:indexPath.row];
            break;
        case 2:
            cell.textLabel.text = [array3 objectAtIndex:indexPath.row];
            break;
        default:
            break;
    }
    return cell;

}

崩溃日志如下:

2012-06-25 23:33:37.694 X-Factor[617:f803] *** Terminating app due to uncaught exception 'NSUnknownKeyException', reason: '[<SettingsViewController 0x6c912a0> setValue:forUndefinedKey:]: this class is not key value coding-compliant for the key tableView.'
*** First throw call stack:
(0x13cd022 0x155ecd6 0x13ccee1 0x9c5022 0x936f6b 0x936edb 0x951d50 0x23971a 0x13cedea 0x13387f1 0x23826e 0xde1fc 0xde779 0xde99b 0xfa0a9 0xf9edd 0xf84aa 0xf834f 0xf9e14 0x13cee99 0x1a14e 0x1a0e6 0x2424bd 0x13cee99 0x1a14e 0x1a0e6 0xc0ade 0xc0fa7 0xc0b13 0x244c48 0x13cee99 0x1a14e 0x1a0e6 0xc0ade 0xc0fa7 0xc0266 0x3f3c0 0x3f5e6 0x25dc4 0x19634 0x12b7ef5 0x13a1195 0x1305ff2 0x13048da 0x1303d84 0x1303c9b 0x12b67d8 0x12b688a 0x17626 0x21c2 0x2135)
terminate called throwing an exception(lldb) 

【问题讨论】:

  • 崩溃日志说什么?有什么错误?如果您知道要查找的内容,它可能比阅读所有这些代码更快
  • 这并不能回答您的问题,但由于您有三个包含数据的数组,您可能希望在numberOfSectionsInTableView: 中输入return 3。
  • array'n' 属性是如何定义的?
  • 这看起来像是一个一致的编码问题 - 您没有在 switch 语句中的每个 case 下放置“break”语句。
  • 我们需要更多详细信息 - 您能否发布崩溃日志的前 40 行(从文件顶部开始)?另外,您能否按照以下链接中的说明告诉我们它在哪一行崩溃? blog.manbolo.com/2012/01/23/xcode-tips-1-break-on-exceptions

标签: ios objective-c xcode uitableview


【解决方案1】:

几点:

a) 我建议您使用 ARC 而不是手动内存管理(Apple 也是如此)。 ARC 是 Objective-C 的未来,您越早开始考虑对象图而不是保留/释放越好。

b) 不要在初始化代码中使用属性,而是使用 ivars(代码假定您将 ivars 合成为下划线)

self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
    _title = @"Settings";
    _tabBarItem.image = [UIImage imageNamed:@"19-gear.png"];
}
return self;

c) 您需要返回 tableview (3) 中的节数,而不是您输入的 2

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

d) 您的 tableView:numberOfRowsInSection 方法不一致。在每个 switch 语句中放置一个 break 语句。即:

switch (section) {
    case 0:
        return [array1 count];
        break;
    case 1:
        return [array2 count];
        break;
    case 2:
        return [array3 count];
        break;
    default:
        break;
}
return 0;

【讨论】:

【解决方案2】:

这不会解决您的崩溃问题,但您会遇到其他问题,除非您在后面加上 break;:

case 1:
    cell.textLabel.text = [array2 objectAtIndex:indexPath.row];

【讨论】:

    【解决方案3】:

    这可能是时间问题 - 当需要填充表格视图时,数据还没有准备好读取。尝试在 initWithNibName (self.array1,self.array2 & self.array3) 中删除 NSMutableArrays,并在 viewWillAppear 而不是 viewDidLoad 中准备数据。另外,当提到保留属性时(我假设 self.array1 等被定义为保留?) - 使用 self.array1

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2010-12-26
      • 2012-09-24
      • 1970-01-01
      • 2012-12-29
      • 2011-12-21
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多