0
votes

I tried a lot but can't kindly help me out of from this situation

//
//  TableViewController.m
//  jsonplaceholder
//
//  Created by manikandan on 19/04/16.
//  Copyright (c) 2016 manikandan. All rights reserved.
//

#import "TableViewController.h"
#import "TableViewCell.h"
#import "DetailViewController.h"

@interface TableViewController ()
<NSURLConnectionDelegate>
{
    NSString *string;
    NSURL *url;
    NSURLRequest *request;
    NSURLConnection *connection;
    NSMutableData *receivedata;

    NSString *imgstring;
    NSURL *imgurl;

    NSMutableData *imgreceivedata;

       NSMutableArray *dictarry;
     NSMutableArray *dictarry1;
     NSMutableArray *dictarry2;
     NSMutableArray *deldictarray;
     NSMutableArray *deldictarray1;
     NSMutableArray *deldictarray2;

    NSMutableDictionary *dict;
    int a;
}

@end

@implementation TableViewController

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

    }
    return self;
}

- (void)viewDidLoad
{

        [super viewDidLoad];
        string =@"http://jsonplaceholder.typicode.com/posts";
        url = [NSURL URLWithString:string];
        request=[NSURLRequest requestWithURL:url cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:60.0];
        connection =[NSURLConnection connectionWithRequest:request delegate:self];
        receivedata =[NSMutableData data];
        NSLog(@"hai");
       imgstring = @"http://2.bp.blogspot.com/-uhWTslN7wO8/UdXSIswnW4I/AAAAAAAACWo/Y10XXAfoGI4/s575/AJITH-OPTIMIZATION+(10).jpg";
        imgurl =[NSURL URLWithString:imgstring];
        imgreceivedata = [NSData dataWithContentsOfURL:imgurl];

}
- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response
{
    [receivedata setLength:0];
}

- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data
{

    [receivedata appendData:data];

}

- (void)connectionDidFinishLoading:(NSURLConnection *)connection
{
    dict=[NSJSONSerialization JSONObjectWithData:receivedata options:NSJSONReadingAllowFragments error:nil];
    NSLog(@"%@",dict);
    dictarry =[dict valueForKey:@"id"];
    dictarry1 =[dict valueForKey:@"title"];
    dictarry2 =[dict valueForKey:@"body"];
    a=[dictarry count];
    NSLog(@"%d",a);
    NSLog(@"%@",dictarry);
    NSLog(@"%@",dictarry1);
    NSLog(@"%@",dictarry2);
    [self.tableView reloadData];

}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}



- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    // Return the number of sections.
    return 1;
}

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

    // Return the number of rows in the section.
    return a;

}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *Cellid=@"Cell";
    TableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:Cellid forIndexPath:indexPath];


   cell.lbltitle.text=[NSString stringWithFormat:@"%@",[dictarry objectAtIndex:indexPath.row ]];
    cell.lblsubtitle .text=[dictarry1 objectAtIndex:indexPath.row];
    cell.lbldescription.text=[dictarry2 objectAtIndex:indexPath.row];
    cell.imgview .image=[UIImage imageWithData:imgreceivedata];

    return cell;
}


-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    DetailViewController *detail =[self.storyboard instantiateViewControllerWithIdentifier:@"detail"];

    detail.passstring1=[[dictarry objectAtIndex:indexPath.row] stringValue] ;
    detail.passstring2=[dictarry1 objectAtIndex:indexPath.row];
    detail.passstring3=[dictarry2 objectAtIndex:indexPath.row];
    detail.passstring4=imgreceivedata;

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

}



- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath
{
    // Return NO if you do not want the specified item to be editable.
    return YES;
}

  /* code problem here i can't  resolve it kindly help me.`enter code here`*/
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (editingStyle == UITableViewCellEditingStyleDelete) {

 // I  just create 3 array to transfer value    
    a=a-1;
    deldictarray=[[NSMutableArray alloc]initWithArray:dictarry];
    deldictarray1=[[NSMutableArray alloc]initWithArray:dictarry1];
    deldictarray2=[[NSMutableArray alloc]initWithArray:dictarry2];

    [deldictarray1 removeObjectAtIndex:[indexPath row]];
    [deldictarray2 removeObjectAtIndex:[indexPath row ]];
    [deldictarray removeObjectAtIndex:[indexPath row]];

   [tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];
   dictarry=[[NSMutableArray alloc]initWithArray:deldictarray];
   dictarry1=[[NSMutableArray alloc]initWithArray:deldictarray1];
    dictarry2=[[NSMutableArray alloc]initWithArray:deldictarray2];
    NSLog(@"%lu",(unsigned long)[dictarry count]);
                 [self.tableView reloadData];



    } else if (editingStyle == UITableViewCellEditingStyleInsert) {
        // Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view
    }   
}


@end

//here i attached my log report also

2016-04-21 10:51:27.046 jsonplaceholder[469:60b] -[NSArrayI removeObjectAtIndex:]: unrecognized selector sent to instance 0x8c3c650 2016-04-21 10:51:27.137 jsonplaceholder[469:60b] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[__NSArrayI removeObjectAtIndex:]: unrecognized selector sent to instance 0x8c3c650' *** First throw call stack: ( 0 CoreFoundation 0x017f11e4 __exceptionPreprocess + 180 1 libobjc.A.dylib
0x015708e5 objc_exception_throw + 44 2 CoreFoundation
0x0188e243 -[NSObject(NSObject) doesNotRecognizeSelector:] + 275 3
CoreFoundation 0x017e150b ___forwarding_
+ 1019 4 CoreFoundation 0x017e10ee _CF_forwarding_prep_0 + 14 5 jsonplaceholder 0x00003d02 -[TableViewController tableView:commitEditingStyle:forRowAtIndexPath:] + 178 6 UIKit
0x0032a6a3 -[UITableView animateDeletionOfRowWithCell:] + 107 7
UIKit 0x004b3595 -[UITableViewCell _swipeDeleteButtonPushed] + 70 8 libobjc.A.dylib 0x01582880 -[NSObject performSelector:withObject:withObject:] + 77 9 UIKit 0x002323b9 -[UIApplication sendAction:to:from:forEvent:] + 108 10 UIKit
0x00232345 -[UIApplication sendAction:toTarget:fromSender:forEvent:] + 61 11 UIKit 0x00333bd1 -[UIControl sendAction:to:forEvent:] + 66 12 UIKit
0x00333fc6 -[UIControl _sendActionsForEvents:withEvent:] + 577 13 UIKit 0x00333243 -[UIControl touchesEnded:withEvent:] + 641 14 UIKit
0x005c82e3 _UIGestureRecognizerUpdate + 7166 15 UIKit
0x00271a5a -[UIWindow _sendGesturesForEvent:] + 1291 16 UIKit
0x00272971 -[UIWindow sendEvent:] + 1021 17 UIKit
0x002445f2 -[UIApplication sendEvent:] + 242 18 UIKit
0x0022e353 _UIApplicationHandleEventQueue + 11455 19 CoreFoundation 0x0177a77f CFRUNLOOP_IS_CALLING_OUT_TO_A_SOURCE0_PERFORM_FUNCTION + 15 20 CoreFoundation 0x0177a10b __CFRunLoopDoSources0 + 235 21 CoreFoundation 0x017971ae __CFRunLoopRun + 910 22 CoreFoundation
0x017969d3 CFRunLoopRunSpecific + 467 23 CoreFoundation
0x017967eb CFRunLoopRunInMode + 123 24 GraphicsServices
0x037e55ee GSEventRunModal + 192 25 GraphicsServices
0x037e542b GSEventRun + 104 26 UIKit
0x00230f9b UIApplicationMain + 1225 27 jsonplaceholder
0x0000411d main + 141 28 libdyld.dylib
0x01e3870d start + 1 ) libc++abi.dylib: terminating with uncaught exception of type NSException (lldb)

3
I never see you decrement the integer you're using to return numberOfRowsInSection. Ideally you wouldn't maintain the number of items as a separate int (should probably be NSInteger anyway) but count the items in your data source. e.g. [dictarry count] (also dictarry is not an ideal name for an array). If that isn't an option you need to decrement a after you remove the object with a--;Dare
after i changed still i have that problemThamizhan Manikandan

3 Answers

2
votes

First of all: Objective-C is an object oriented language. Use a custom class instead of three arrays as data source.

To solve your issue forget that a variable and return always the number of items in dictarry

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return dictarry.count;
}

And delete the line to reload the table view in commitEditingStyle because deleteRowsAtIndexPaths rearranges the table view automatically.

Edit

Your crash log reveals that the three data source arrays are immutable. Although they are declared as NSMutabeArray, the KVC method valueForKey: returns an immutable array. Change these three lines:

dictarry = [[dict valueForKey:@"id"] mutableCopy];
dictarry1 = [[dict valueForKey:@"title"] mutableCopy];
dictarry2 = [[dict valueForKey:@"body"] mutableCopy];
0
votes

Firstly, the value you are passing to your mutable array, that array is not mutable. Hence, removeObjectAtIndex: is failing.

Please add the values as:

dictarry  = [[dict valueForKey:@"id"] mutableCopy]; 

Thus the array becomes mutable.

-1
votes

Please use the assignments as follows:

dictarry = [[dict valueForKey:@"id"] mutableCopy];
dictarry1 = [[dict valueForKey:@"title"] mutableCopy];
dictarry2 = [[dict valueForKey:@"body"] mutableCopy];