我尝试了很多,但不能帮助我摆脱这种情况
//
// 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
//这里我附上了我的日志报告
2016-04-21 10:51:27.046 jsonplaceholder [469:60b] - [ NSArrayI removeObjectAtIndex:]:发送到实例的无法识别的选择器 0x8c3c650 2016-04-21 10:51:27.137 jsonplaceholder [469:60b] *** 由于未捕获的异常而终止应用程序 &#39; NSInvalidArgumentException&#39;,原因:&#39; - [__ NSArrayI removeObjectAtIndex:]:发送到实例的无法识别的选择器 0x8c3c650&#39; ***第一次抛出调用堆栈:(0 CoreFoundation 0x017f11e4 __exceptionPreprocess + 180 1 libobjc.A.dylib
0x015708e5 objc_exception_throw + 44 2 CoreFoundation
0x0188e243 - [NSObject(NSObject)doesNotRecognizeSelector:] + 275 3
CoreFoundation 0x017e150b ___转发_ + 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图形服务
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:以uncaught结束 NSException类型的异常(lldb)
答案 0 :(得分:2)
首先:Objective-C是一种面向对象的语言。使用自定义类而不是三个数组作为数据源。
要解决您的问题,请忘记a
变量,并始终返回dictarry
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return dictarry.count;
}
并删除该行以重新加载commitEditingStyle
中的表格视图,因为deleteRowsAtIndexPaths
会自动重新排列表格视图。
修改强>
您的崩溃日志显示三个数据源数组是不可变的。尽管它们被声明为NSMutabeArray
,但KVC方法valueForKey:
返回一个不可变数组。改变这三行:
dictarry = [[dict valueForKey:@"id"] mutableCopy];
dictarry1 = [[dict valueForKey:@"title"] mutableCopy];
dictarry2 = [[dict valueForKey:@"body"] mutableCopy];
答案 1 :(得分:0)
首先,您传递给可变数组的值,该数组不可变。因此,removeObjectAtIndex:失败。
请将值添加为:
dictarry = [[dict valueForKey:@"id"] mutableCopy];
因此阵列变得可变。
答案 2 :(得分:-1)
请按如下方式使用作业:
dictarry = [[dict valueForKey:@"id"] mutableCopy];
dictarry1 = [[dict valueForKey:@"title"] mutableCopy];
dictarry2 = [[dict valueForKey:@"body"] mutableCopy];