Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/109.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/amazon-s3/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 在tableview中创建行时出错_Ios_Dynamic_Tableview_Rows - Fatal编程技术网

Ios 在tableview中创建行时出错

Ios 在tableview中创建行时出错,ios,dynamic,tableview,rows,Ios,Dynamic,Tableview,Rows,在tableview中创建行时出错 我的表视图加载了json 我在这里看到了很多例子,但是没有一个能够解决 我的代码 NSMutableArray *myArray = [NSMutableArray arrayWithArray:news]; [myArray insertObject:@"teste" atIndex:0]; NSMutableArray *path = [NSMutableArray arrayWithObject:[NSIndexPath

在tableview中创建行时出错

我的表视图加载了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\u endCellAnimationsWithContext:],/SourceCache/UIKit\u Sim/UIKit-2380.17/UITableView.m:908中断言失败 2013-05-30 23:15:17.347 lerJson[1141:c07]由于未捕获的异常“NSinternalinconsistenceexception”而终止应用程序,原因是:“尝试将第11行插入第1节,但更新后只有1节” **第一次抛出调用堆栈: (0x1c95012 0x10d2e7e 0x1c94e78 0xb68665 0xb670b 0xc4945 0xc4973 0x476d 0x76d78 0x789B 0x2e185a 0x2e099b 0x2e20df 0x2e4d2d 0x2e4cac 0x2dca28 0x49972 0x49e53 0x27d4a 0x19698 0x1BF0DF09 0x1bf0ad0 0x1c0abf5 0x1c0a962 0x1C3BB6 0x1c3af44 0x1c3ae1b 0x1bef7e3 0x1bef668 0x16FF26AD5) libc++abi.dylib:terminate调用引发异常

我的完整代码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
#导入“j2_ViewController.h”
@接口j2_ViewController()
@结束
@视图控制器的实现
-(id)initWithStyle:(UITableViewStyle)样式
{
self=[super initWithStyle:style];
如果(自我){
//自定义初始化
}
回归自我;
}
-(无效)viewDidLoad
{
[超级视图下载];
[自娱自乐];
}
-(无效)carregaDados
{
[UIApplication sharedApplication].networkActivityIndicatorVisible=是;
NSURL*url2=[[NSURL alloc]initWithString:[@]http://localhost:3000/json.aspx?ind=0&tot=12“stringByAddingPercentEscapesUsingEncoding:NSISOLatin1StringEncoding]];
NSURLRequest*request=[nsurlRequestRequestWithURL:url2];
[[NSURLConnection alloc]initWithRequest:请求委托:self];
}
-(无效)carregaDados2
{
[UIApplication sharedApplication].networkActivityIndicatorVisible=是;
NSURL*url2=[[NSURL alloc]initWithString:[@]http://localhost:3000/json.aspx?ind=12&tot=12“stringByAddingPercentEscapesUsingEncoding:NSISOLatin1StringEncoding]];
NSURLRequest*request=[nsurlRequestRequestWithURL:url2];
NSData*响应=[NSURLConnection sendSynchronousRequest:request returningResponse:nil错误:nil];
N错误*jsonParsingError=nil;
NSMutableData*myData=[[NSMutableData alloc]init];
[myData:response];
NSMutableArray*news2;
news2=[NSJSONSerialization JSONObjectWithData:myData选项:无错误:&jsonParsingError];
//NSLog(@“LOG:%@”,新闻2);
NSMutableArray*myArray=[NSMutableArray数组WithArray:news];
[myArray addObjectsFromArray:news2];
NSLog(@“LOG:%@”,myArray);
新闻=我的数组;
//NSLog(@“Erro:%@”,jsonParsingError);
[tableViewjson重新加载数据];
}
-(void)连接:(NSURLConnection*)连接DidReceiverResponse:(NSURResponse*)响应
{
数据=[[NSMutableData alloc]init];
}
-(void)连接:(NSURLConnection*)连接未接收数据:(NSData*)数据
{
[数据附录数据:数据];
}
-(无效)连接IDFinishLoading:(NSURLConnection*)连接
{
[UIApplication sharedApplication].networkActivityIndicatorVisible=否;
n错误*e=零;
news=[NSJSONSerialization JSONObjectWithData:数据选项:无错误:&e];
[tableViewjson重新加载数据];
//NSLog(@“erro=%@),e);
}
-(无效)连接:(NSURLConnection*)连接失败错误:(NSError*)错误
{
UIAlertView*alert=[[UIAlertView alloc]initWithTitle:@“Erro”消息:@“Não foi possivile fazer o download-支票sua conexão 3g ou wi-fi”代表:无取消按钮:@“取消”其他按钮:无];
[警报显示];
[UIApplication sharedApplication].networkActivityIndicatorVisible=否;
}
-(无效)未收到记忆警告
{
[超级记忆警告];
//处置所有可以重新创建的资源。
}
#pragma标记-表视图数据源
-(NSInteger)表格视图中的节数:(UITableView*)表格视图
{
返回1;
}
-(NSInteger)表视图:(UITableView*)表视图行数节:(NSInteger)节
{
[UIApplication sharedApplication].networkActivityIndicatorVisible=否;
NSInteger teste=[新闻计数];
返回测试;
}
-(UITableViewCell*)tableView:(UITableView*)tableView cellForRowAtIndexPath:(NSIndexPath*)indexPath
{
静态NSString*CellIdentifier=@“celula”;
UITableViewCell*单元格=[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
如果(单元格==nil)
{
cell=[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault重用标识符:CellIdentifier];
}
cell.textlab.text=[[news objectAtIndex:indexath.row]objectForKey:@“nome”];
返回单元;
}
//查马·埃萨·芬索昂·昆多·阿蒂奇·苏决赛
-(void)ScrollViewDiEndDraging:(UIScrollView*)scrollView将减速:(BOOL)减速
{
NSInteger currentOffset=scrollView.contentOffset.y;
NSInteger maximumOffset=scrollView.contentSize.height-scrollView.frame.size.height;

如果(maximumOffset-currentOffset,您还需要像这样更新数据源:
[新闻插入对象:@“teste”索引:0];

我想你只有一个部分

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

对于此代码:
NSMutableArray*path=[NSMutableArray arrayWithObject:[nsindepath indexPathForRow:[news count]instation:0]];


节和行从0开始。

您有多少节?也许您可以发布tableview委托和数据源代码节从0开始,而不是1。此外,您还可以添加
@“teste”
到临时数组。在调用
endUpdates
之前,您需要将其添加到数据源方法使用的实际数组中。我进行了更改,返回了以下错误:由于未捕获的异常“NSInternalInconsistencyException”终止应用程序,原因:“无效更新:第0节中的行数无效。数字无效。”