gpt4 book ai didi

ios - 如何从 TableView 中删除行?

转载 作者:行者123 更新时间:2023-11-29 01:03:19 24 4
gpt4 key购买 nike

我尝试了很多,但无法帮助我摆脱这种情况

//
// 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:]: 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)

最佳答案

首先:Objective-C 是一种面向对象的语言。使用自定义类而不是三个数组作为数据源。

要解决您的问题,请忘记 a 变量并始终返回 dictarry 中的项目数

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

并在 commitEditingStyle 中删除重新加载 TableView 的行,因为 deleteRowsAtIndexPaths 会自动重新排 ListView 。

编辑

您的崩溃日志显示三个数据源数组是不可变的。尽管它们被声明为 NSMutabeArray,但 KVC 方法 valueForKey: 返回一个不可变数组。更改这三行:

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

关于ios - 如何从 TableView 中删除行?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36745531/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com