在表格中创建行时出错 [英] Error creating row in tableview

查看:235
本文介绍了在表格中创建行时出错的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在tableview中创建行时出错



我的表视图加载了一个json。



很多例子在这里,但是没有人能够解决



我的代码

  NSMutableArray * myArray = [NSMutableArray arrayWithArray:news]; 
[myArray insertObject:@testeatIndex:0];
NSMutableArray * path = [NSMutableArray arrayWithObject:[NSIndexPath indexPathForRow:[news count] -1 inSection:1]];
[self.tableView insertRowsAtIndexPaths:path withRowAnimation:UITableViewRowAnimationAutomatic];
[self.tableView endUpdates];

错误显示



2013-05-30 23:15:17.345 lerJson [1141:c07] *断言失败 - [UITableView _endCellAnimationsWithContext:],/SourceCache/UIKit_Sim/UIKit-2380.17/UITableView.m:908
2013-05-30 23:15:17.347 lerJson [1141:c07]
由于未捕获的异常'NSInternalInconsistencyException'终止应用程序,原因:'尝试将第11行插入到第1部分,但只有更新
之后1个区段*
*第一掷调用堆栈:
(0x1c95012 0x10d2e7e 0x1c94e78 0xb68665 0xb670b 0xc4945 0xc4973 0x476d 0x76d78 0x789eb 0x2e185a 0x2e099b 0x2e20df 0x2e4d2d 0x2e4cac 0x2dca28 0x49972 0x49e53 0x27d4a 0x19698 0x1bf0df9 0x1bf0ad0 0x1c0abf5 0x1c0a962 0x1c3bbb6 0x1c3af44 0x1c3ae1b 0x1bef7e3 0x1bef668 0x16ffc 0x26ad 0x25d5)
libc ++ abi.dylib:终止调用抛出异常



我的完整代码j2_ViewController.h

  #importj2_ViewController.h 

@interface j2_ViewController()

@end

@implementation j2_ViewController



- (id)initWithStyle:(UITableViewStyle)style
{
self = [super initWithStyle:style];
if(self){
//自定义初始化
}
return self;
}

- (void)viewDidLoad
{
[super viewDidLoad];

[self carregaDados];

}

- (void)carregaDados
{
[UIApplication sharedApplication] .networkActivityIndi​​catorVisible = YES;


NSURL * url2 = [[NSURL alloc] initWithString:[@http:// localhost:3000 / json.aspx?ind = 0& tot = 12stringByAddingPercentEscapesUsingEncoding:NSISOLatin1StringEncoding] ]。

NSURLRequest * request = [NSURLRequest requestWithURL:url2];

[[NSURLConnection alloc] initWithRequest:request delegate:self];
}



- (void)carregaDados2
{
[UIApplication sharedApplication] .networkActivityIndi​​catorVisible = YES;


NSURL * url2 = [[NSURL alloc] initWithString:[@http:// localhost:3000 / json.aspx?ind = 12& tot = 12stringByAddingPercentEscapesUsingEncoding:NSISOLatin1StringEncoding] ]。

NSURLRequest * request = [NSURLRequest requestWithURL:url2];
NSData * response = [NSURLConnection sendSynchronousRequest:request returnedResponse:nil error:nil];
NSError * jsonParsingError = nil;

NSMutableData * myData = [[NSMutableData alloc] init];
[myData appendData:response];


NSMutableArray * news2;

news2 = [NSJSONSerialization JSONObjectWithData:myData选项:nil错误:& jsonParsingError];
// NSLog(@LOG:%@,news2);


NSMutableArray * myArray = [NSMutableArray arrayWithArray:news];
[myArray addObjectsFromArray:news2];

NSLog(@LOG:%@,myArray);

news = myArray;

// NSLog(@Erro:%@,jsonParsingError);
[tableViewjson reloadData];


- (void)连接:( NSURLConnection *)连接didReceiveResponse :( NSURLResponse *)响应
{
data = [[NSMutableData alloc] init];
}

- (void)连接:(NSURLConnection *)连接didReceiveData:(NSData *)theData
{
[data appendData:theData];
}

- (void)connectionDidFinishLoading :( NSURLConnection *)连接
{
[UIApplication sharedApplication] .networkActivityIndi​​catorVisible = NO;

NSError * e = nil;
news = [NSJSONSerialization JSONObjectWithData:data options:nil error:& e];

[tableViewjson reloadData];
// NSLog(@erro =%@,e); $($)

- (void)连接:(NSURLConnection *)连接didFailWithError :( NSError *)错误
{
UIAlertView * alert = [[ UIAlertView alloc] initWithTitle:@Erro消息:@Nãofoi possivle fazer o download - check suaconexão3g ou wi-fidelegate:nil cancelButtonTitle:@cancelarotherButtonTitles:nil];
[alert show];
[UIApplication sharedApplication] .networkActivityIndi​​catorVisible = NO;
}

- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
//处理可以重新创建的任何资源。
}

#pragma mark - 表视图数据源

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
[UIApplication sharedApplication] .networkActivityIndi​​catorVisible = NO;
NSInteger teste = [新闻计数];
return teste;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath :( NSIndexPath *)indexPath
{
static NSString * CellIdentifier = @celula ;

UITableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if(cell == nil)
{
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}

cell.textLabel.text = [[news objectAtIndex:indexPath.row] objectForKey:@nome];

返回单元格;
}

// chama essafunçãoquando o scroll atinge seu final
- (void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate((BOOL))减速
{

NSInteger currentOffset = scrollView.contentOffset.y;
NSInteger maximumOffset = scrollView.contentSize.height - scrollView.frame.size.height;

if(maximumOffset - currentOffset< = -40)
{
// carega mais dados
// [self carregaDados2];
[self.tableView beginUpdates];



NSMutableArray * myArray = [NSMutableArray arrayWithArray:news];
[myArray insertObject:@testeatIndex:0];
NSMutableArray * path = [NSMutableArray arrayWithObject:[NSIndexPath indexPathForRow:[news count] -1 inSection:1]];
[self.tableView insertRowsAtIndexPaths:path withRowAnimation:UITableViewRowAnimationAutomatic];
[self.tableView endUpdates];
}
}



@end

我的代理没有改变

解决方案

您还需要更新数据源,如下所示:
[news insertObject:@testeatIndex:0];



我假设你只有1个部分。



更改此代码: NSMutableArray * path = [NSMutableArray arrayWithObject:[NSIndexPath indexPathForRow:[news count] inSection: 1 ]];



到此代码: NSMutableArray * path = [NSMutableArray arrayWithObject:[NSIndexPath indexPathForRow:[news count] inSection: 0 ]];



部分和行从0开始。 $ b

Error creating row in tableview.

My table view is loaded with a json.

I saw many examples here, however none were able to solve

my code

NSMutableArray *myArray = [NSMutableArray arrayWithArray:news];
        [myArray insertObject:@"teste" atIndex:0];
        NSMutableArray *path = [NSMutableArray arrayWithObject:[NSIndexPath indexPathForRow:[news count]-1 inSection:1]];
        [self.tableView insertRowsAtIndexPaths:path withRowAnimation:UITableViewRowAnimationAutomatic];
        [self.tableView endUpdates];

error displayed

2013-05-30 23:15:17.345 lerJson[1141:c07] * Assertion failure in -[UITableView _endCellAnimationsWithContext:], /SourceCache/UIKit_Sim/UIKit-2380.17/UITableView.m:908 2013-05-30 23:15:17.347 lerJson[1141:c07] Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'attempt to insert row 11 into section 1, but there are only 1 sections after the update' ** First throw call stack: (0x1c95012 0x10d2e7e 0x1c94e78 0xb68665 0xb670b 0xc4945 0xc4973 0x476d 0x76d78 0x789eb 0x2e185a 0x2e099b 0x2e20df 0x2e4d2d 0x2e4cac 0x2dca28 0x49972 0x49e53 0x27d4a 0x19698 0x1bf0df9 0x1bf0ad0 0x1c0abf5 0x1c0a962 0x1c3bbb6 0x1c3af44 0x1c3ae1b 0x1bef7e3 0x1bef668 0x16ffc 0x26ad 0x25d5) libc++abi.dylib: terminate called throwing an exception

My full code j2_ViewController.h

#import "j2_ViewController.h"

@interface j2_ViewController ()

@end

@implementation j2_ViewController



- (id)initWithStyle:(UITableViewStyle)style
{
    self = [super initWithStyle:style];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];

    [self carregaDados];

}

-(void)carregaDados
{
    [UIApplication sharedApplication].networkActivityIndicatorVisible = YES;


    NSURL *url2 = [[NSURL alloc] initWithString:[@"http://localhost:3000/json.aspx?ind=0&tot=12" stringByAddingPercentEscapesUsingEncoding:NSISOLatin1StringEncoding]];

    NSURLRequest *request = [NSURLRequest requestWithURL:url2];

    [[NSURLConnection alloc] initWithRequest:request delegate:self];
}



-(void)carregaDados2
{
    [UIApplication sharedApplication].networkActivityIndicatorVisible = YES;


    NSURL *url2 = [[NSURL alloc] initWithString:[@"http://localhost:3000/json.aspx?ind=12&tot=12" stringByAddingPercentEscapesUsingEncoding:NSISOLatin1StringEncoding]];

    NSURLRequest *request = [NSURLRequest requestWithURL:url2];
    NSData *response = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil];
    NSError *jsonParsingError = nil;

    NSMutableData *myData = [[NSMutableData alloc]init];
    [myData appendData:response];


    NSMutableArray *news2;

    news2 = [NSJSONSerialization JSONObjectWithData:myData options:nil error:&jsonParsingError];
    //NSLog(@"LOG: %@", news2);


    NSMutableArray *myArray = [NSMutableArray arrayWithArray:news];
    [myArray addObjectsFromArray:news2];

    NSLog(@"LOG: %@", myArray);

    news = myArray;

    //NSLog(@"Erro: %@", jsonParsingError);
    [tableViewjson reloadData];
}

- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response
{
    data = [[NSMutableData alloc]init];
}

- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)theData
{
    [data appendData:theData];
}

- (void)connectionDidFinishLoading:(NSURLConnection *)connection
{
    [UIApplication sharedApplication].networkActivityIndicatorVisible = NO;

    NSError *e = nil;
    news = [NSJSONSerialization JSONObjectWithData:data options:nil error:&e];

    [tableViewjson reloadData];
    //NSLog(@"erro=%@",e);

}

- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error
{
    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Erro" message:@"Não foi possivle fazer o download - cheque sua conexão 3g ou wi-fi" delegate:nil cancelButtonTitle:@"cancelar" otherButtonTitles:nil];
    [alert show];
    [UIApplication sharedApplication].networkActivityIndicatorVisible = NO;
}

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

#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    [UIApplication sharedApplication].networkActivityIndicatorVisible = NO;
    NSInteger teste = [news count];
    return teste;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"celula";

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if(cell == nil)
    {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
    }

    cell.textLabel.text = [[news objectAtIndex:indexPath.row] objectForKey:@"nome"];

    return cell;
}

//chama essa função quando o scroll atinge seu final
-(void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate
{

    NSInteger currentOffset = scrollView.contentOffset.y;
    NSInteger maximumOffset = scrollView.contentSize.height - scrollView.frame.size.height;

    if(maximumOffset - currentOffset <= -40)
    {
       //carega mais dados
        //[self carregaDados2];
        [self.tableView beginUpdates];



        NSMutableArray *myArray = [NSMutableArray arrayWithArray:news];
        [myArray insertObject:@"teste" atIndex:0];
        NSMutableArray *path = [NSMutableArray arrayWithObject:[NSIndexPath indexPathForRow:[news count]-1 inSection:1]];
        [self.tableView insertRowsAtIndexPaths:path withRowAnimation:UITableViewRowAnimationAutomatic];
        [self.tableView endUpdates];
    }
}



@end

My delegation has not changed

解决方案

You also need to update your datasource like this: [news insertObject:@"teste" atIndex:0];

I assume you only have 1 section.

change this code: NSMutableArray *path = [NSMutableArray arrayWithObject:[NSIndexPath indexPathForRow:[news count] inSection:1]];

to this code: NSMutableArray *path = [NSMutableArray arrayWithObject:[NSIndexPath indexPathForRow:[news count] inSection:0]];

sections and rows starts at 0.

这篇关于在表格中创建行时出错的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆