【问题标题】:Error creating row in tableview在 tableview 中创建行时出错
【发布时间】:2013-05-26 18:44:24
【问题描述】:

在表格视图中创建行时出错。

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

我在这里看到了很多例子,但是没有一个能够解决

我的代码

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];

错误显示

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

#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

我的代表团没有改变

【问题讨论】:

  • 你有多少个部分?也许你可以发布你的 tableview 委托和数据源代码
  • 节从 0 开始,而不是 1。此外,您将 @"teste" 添加到临时数组中。在调用 endUpdates 之前,您需要将其添加到数据源方法使用的实际数组中。

标签: ios dynamic tableview rows


【解决方案1】:

您还需要像这样更新数据源: [news insertObject:@"teste" atIndex:0];

我假设您只有 1 个部分。

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

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

节和行从 0 开始。

【讨论】:

  • 我进行了更改,返回以下错误:由于未捕获的异常“NSInternalInconsistencyException”而终止应用程序,原因:“无效更新:第 0 节中的行数无效。现有文件中包含的行数更新后的节 (12) 必须等于更新前该节中包含的行数 (12),加上或减去从该节插入或删除的行数(1 插入,0 删除)加或减移入或移出该部分的行数(0移入,0移出)。'
  • @leedream 在您的问题下查看我之前的评论。您没有正确更新数据源数组。这就是这个错误的原因。
  • @leedream 更新了我的答案,请检查,rmaddy 是对的,您还需要在更新 tableview 之前更新您的数据源
  • 我看到什么返回:2013-05-31 09:28:26.479 lerJson[538:c07] 例外:尝试将第 12 行插入第 0 部分,但之后第 0 部分只有 12 行更新
  • [news insertObject:@"teste" atIndex:0];返回错误:: 2013-05-31 10:03:25.180 lerJson [670: c07] 异常: - [__NSCFArray insertObject: atIndex]: método mutatione enviado para imutável object
猜你喜欢
  • 2020-05-01
  • 2019-05-24
  • 2016-09-23
  • 2016-09-19
  • 1970-01-01
  • 1970-01-01
  • 2011-06-14
  • 2015-05-28
  • 2015-07-03
相关资源
最近更新 更多