簡體   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