繁体   English   中英

UITableView在滚动,添加或删除单元格时崩溃

[英]UITableView Crash on scroll , add or delete cell

我刚刚将填充方法更改为使用plist,在尝试添加或删除单元格时导致崩溃。 滚动表也会导致崩溃。 我没有使用IB,而是全部以编程方式创建。 搜索问题将导致没有答案或与IB相关的解决方案。 我所拥有的代码是

//****************** Set Table Data



NSString *filePath = [[NSBundle mainBundle] pathForResource:@"stuff" ofType:@"plist"];
NSMutableDictionary *dict = [[NSMutableDictionary alloc] initWithContentsOfFile:filePath];
devices = [NSMutableArray arrayWithArray:[dict objectForKey:@"Devices"]];


//****************** Customize Number of Rows in Table


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {

    int count = [devices count];
    if(self.editing) count++;
    return count;
}


//****************** Customize the Cells


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:[NSString stringWithFormat:@"Cell %i",indexPath.section]];

    if (cell == nil) {

    cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:[NSString stringWithFormat:@"Cell %i",indexPath.section]] autorelease];

}

    switch(indexPath.section){
    case 0:{ cell.textLabel.text = [devices objectAtIndex: indexPath.row];} break;
    default: break;
}


    return cell;

}

这就是我认为相关的代码,因为在从plist填充之前,一切都很好。

已请求更多代码

 //***************** Allow Editing of Cells

  - (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{

 [devices removeObjectAtIndex:indexPath.row];
 [myTable reloadData];

}



//****************** Add New Cell If ok button is pressed

- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex {

UITextField * newDevice = [alertView textFieldAtIndex:0];
        NSLog(@"newDevice - %@",newDevice.text);

if (alertView.tag == 1 && buttonIndex == 1){
[devices addObject: newDevice.text];
[myTable reloadData];

UPDATE

注释掉我的数据加载可以解决崩溃问题,这使我相信我错误地调用了加载数据的方式。 当然,我会对此进行研究,并欣赏一下他的两分钱投入到更大的大脑中。

switch

 switch(indexPath.section){
   case 0:{ 
       if ([devices count] > indexPath.row) {
            cell.textLabel.text = [devices objectAtIndex: indexPath.row];
       }
   } break;
   default: break;
 }

固定。 看来我的阵列被立即自动释放,然后手动保留然后释放,解决了我的问题

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM