简体   繁体   English

TableView选择在iPhone应用程序中崩溃

[英]TableView selection crashing in iPhone app

I have table view with records in. The problem is that when we click on cell according to series like 1, 2, 3, then it works fine, but if we select 2 directly then it crashes. 我有带记录的表视图。问题是,当我们根据像1、2、3这样的序列单击单元格时,它可以正常工作,但是如果我们直接选择2,则它将崩溃。 Here is my code. 这是我的代码。

My tableView has multiple sections having data in sections. 我的tableView有多个节,每个节中都有数据。

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

  {

static NSString *CellIdentifier = @"Cell";

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
    cell = [[[UITableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier:CellIdentifier] autorelease];

    cell.selectionStyle=UITableViewCellSelectionStyleGray;
    cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;


}



if (indexPath.section==0) 

{
    cell.textLabel.font=[UIFont fontWithName:@"Helvetical Bold" size:14];
    cell.textLabel.textColor=[UIColor blackColor];
    ObjectData*coffeeObj=[subCategoryArray objectAtIndex:indexPath.row];
    cell.textLabel.text=coffeeObj.subCategoryTitle;
    return cell;

}


else if (indexPath.section==1) 

{
    cell.textLabel.font=[UIFont fontWithName:@"Helvetical Bold" size:14];
    cell.textLabel.textColor=[UIColor blackColor];
    ObjectData*coffeeObj=[subCategoryArrayOne objectAtIndex:indexPath.row];
    cell.textLabel.text=coffeeObj.subCategoryTitle;
    return cell;

}




else {


    cell.textLabel.font=[UIFont fontWithName:@"Helvetical Bold" size:14];
    cell.textLabel.textColor=[UIColor blackColor];
    ObjectData*coffeeObj=[subCategoryArrayTwo objectAtIndex:indexPath.row];
    cell.textLabel.text=coffeeObj.subCategoryTitle;
    return cell;



}}




    - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {



NSString*title;

if (indexPath.section==0) {




    ObjectData*coffeeObj=[subCategoryArray objectAtIndex:indexPath.row];


    NSString*subCatID =coffeeObj.subCategoryID;


    NSArray *tempArray =[[DataController staticVersion] startParsing:[NSString stringWithFormat:@"http://www.celeritas-solutions.com/pah_brd_v1/productivo/getDetailData.php?subCatID=%@",subCatID]];

    for (int i = 0; i<[tempArray count]; i++) {

        id *item = [tempArray objectAtIndex:i];
        NSDictionary *dict = (NSDictionary *) item;
        ObjectData *theObject =[[ObjectData alloc] init];
        [theObject setCatalogID:[dict objectForKey:@"CatalogID"]];

        [theObject setContentType:[dict objectForKey:@"ContentType"]];
        [theObject setContentTitle:[dict objectForKey:@"ContentTitle"]];
        [theObject setContentDescription:[dict objectForKey:@"ContentDescription"]];
        [theObject setContentSource:[dict objectForKey:@"ContentSource"]];





        [detailArray addObject:theObject];
        [theObject release];
        theObject=nil;






    }




    int counting=[detailArray count];

    NSLog(@"Counting is %d",counting);

    ObjectData*data1=[detailArray objectAtIndex:indexPath.row];

    NSString*content_Type=data1.contentType;
    NSString*content_Description=data1.contentDescription;
    NSString*content_Source=data1.contentSource;
    NSString*content_Title=data1.contentTitle;

    NSLog(@"Type is %@",content_Type);
    NSLog(@"Description is %@",content_Description);
    NSLog(@"Source is %@",content_Source);


    CategoryDetailViewController*targetController=[[CategoryDetailViewController alloc]init];
    targetController.content_Type=content_Type;
    targetController.content_Description=content_Description;
    targetController.content_Source=content_Source;
    targetController.content_Title=content_Title;



    [self.navigationController pushViewController:targetController animated:YES];



        }



else if(indexPath.section==1){



    ObjectData*coffeeObj=[subCategoryArrayOne objectAtIndex:indexPath.row];
    NSString*subCatID =coffeeObj.subCategoryID;
    NSArray *tempArray =[[DataController staticVersion] startParsing:[NSString stringWithFormat:@"http://www.celeritas-solutions.com/pah_brd_v1/productivo/getDetailData.php?subCatID=%@",subCatID]];
    for (int i = 0; i<[tempArray count]; i++) {

        id *item = [tempArray objectAtIndex:i];
        NSDictionary *dict = (NSDictionary *) item;
        ObjectData *theObject =[[ObjectData alloc] init];
        [theObject setCatalogID:[dict objectForKey:@"CatalogID"]];

        [theObject setContentType:[dict objectForKey:@"ContentType"]];
        [theObject setContentTitle:[dict objectForKey:@"ContentTitle"]];
        [theObject setContentDescription:[dict objectForKey:@"ContentDescription"]];
        [theObject setContentSource:[dict objectForKey:@"ContentSource"]];


        [detailArrayOne addObject:theObject];
        [theObject release];
        theObject=nil;


    }

    ObjectData*data1=[detailArrayOne objectAtIndex:indexPath.row];

    NSString*content_Type=data1.contentType;
    NSString*content_Description=data1.contentDescription;
    NSString*content_Source=data1.contentSource;
    NSString*content_Title=data1.contentTitle;

    NSLog(@"Type is %@",content_Type);
    NSLog(@"Description is %@",content_Description);
    NSLog(@"Source is %@",content_Source);


    CategoryDetailViewController*targetController=[[CategoryDetailViewController alloc]init];
    targetController.content_Type=content_Type;
    targetController.content_Description=content_Description;
    targetController.content_Source=content_Source;
    targetController.content_Title=content_Title;



    [self.navigationController pushViewController:targetController animated:YES];








    }
else {




    ObjectData*coffeeObj=[subCategoryArrayTwo objectAtIndex:indexPath.row];


    NSString*subCatID =coffeeObj.subCategoryID;


    NSArray *tempArray =[[DataController staticVersion] startParsing:[NSString stringWithFormat:@"http://www.celeritas-solutions.com/pah_brd_v1/productivo/getDetailData.php?subCatID=%@",subCatID]];

    for (int i = 0; i<[tempArray count]; i++) {

        id *item = [tempArray objectAtIndex:i];
        NSDictionary *dict = (NSDictionary *) item;
        ObjectData *theObject =[[ObjectData alloc] init];
        [theObject setCatalogID:[dict objectForKey:@"CatalogID"]];

        [theObject setContentType:[dict objectForKey:@"ContentType"]];
        [theObject setContentTitle:[dict objectForKey:@"ContentTitle"]];
        [theObject setContentDescription:[dict objectForKey:@"ContentDescription"]];
        [theObject setContentSource:[dict objectForKey:@"ContentSource"]];





        [detailArrayTwo addObject:theObject];
        [theObject release];
        theObject=nil;






    }




    int counting=[detailArray count];

    NSLog(@"Counting is %d",counting);

    ObjectData*data1=[detailArrayTwo objectAtIndex:indexPath.row];

    NSString*content_Type=data1.contentType;
    NSString*content_Description=data1.contentDescription;
    NSString*content_Source=data1.contentSource;
    NSString*content_Title=data1.contentTitle;

    NSLog(@"Type is %@",content_Type);
    NSLog(@"Description is %@",content_Description);
    NSLog(@"Source is %@",content_Source);


    CategoryDetailViewController*targetController=[[CategoryDetailViewController alloc]init];
    targetController.content_Type=content_Type;
    targetController.content_Description=content_Description;
    targetController.content_Source=content_Source;
    targetController.content_Title=content_Title;

    [self.navigationController pushViewController:targetController animated:YES];

    }




   [tableView deselectRowAtIndexPath:indexPath animated:YES];


       }

In didSelectRowAtIndexpath didSelectRowAtIndexpath

addObject: adds the object to array but the reference is same,and you are releasing it at the end of loop Remove addObject:将对象添加到数组,但引用相同,并且在循环结束时将其释放

[theObject release];
 theObject=nil;

add/use 添加/使用

    ObjectData *theObject =[[[ObjectData alloc] init]autorelease];

EDIT 编辑

The error says you are trying to access a value from an array which is not having that number of values in it.It is a range exception 错误提示您正在尝试从数组中访问的值中没有该数量的值,这是范围异常

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

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