Ios insertrowsatindexpath或重载数据不';不回答

Ios insertrowsatindexpath或重载数据不';不回答,ios,uitableview,Ios,Uitableview,这是我的密码。昨天,它工作得很好。但是今天早上,它不起作用了 -(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { if (indexPath.row == 0) { RSDrug *drug = [[RSDrug alloc]init]; [drugArray insertObject:drug atIndex:0];

这是我的密码。昨天,它工作得很好。但是今天早上,它不起作用了

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.row == 0) {
        RSDrug *drug = [[RSDrug alloc]init];
        [drugArray insertObject:drug atIndex:0];
        NSIndexPath *ip = [NSIndexPath indexPathForRow:1 inSection:0];
        [tableView insertRowsAtIndexPaths:@[ip] withRowAnimation:UITableViewRowAnimationAutomatic];
    }
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return drugArray.count + 1;
}
我尝试添加一些代码,比如BeginUpdate/EndUpdate,或者删除InsertRowsatindExpath,只需使用重载数据

但同样的结果。当应用程序运行到insertRowsAtIndexPaths或重载数据时,它卡住了,没有响应

所以我需要一些帮助。如何解决这个问题

发现了问题…………一个愚蠢的错误

添加的单元格中有两个UITextField,它们都具有相同的leftView,我的代码如下:

UIImageView *leftImage = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"img"]];
field_1.leftView = leftImage;
field_2.leftView = leftImage;
我从来不知道我不能重用leftView。如果我将leftImage设置为field1,则无法再将其设置为field2

当我换成这个的时候,它又起作用了

field_1.leftView = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"img"]];
field_2.leftView = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"img"]];
你试过这个吗

RSDrug *drug = [[RSDrug alloc]init];

[drugArray insertObject:drug atIndex:0];
[tableView beginUpdates];

    // indexpath inserted is with the index 0 same with the `insertObject: `
    NSIndexPath *ip = [NSIndexPath indexPathForRow:0 inSection:0];
    [tableView insertRowsAtIndexPaths:@[ip] withRowAnimation:UITableViewRowAnimationAutomatic];

    //here is what i use to do instead of @[]
    //[tableView insertRowsAtIndexPaths:[NSArray arrayWithObject:ip] withRowAnimation:UITableViewRowAnimationAutomatic];

[tableView endUpdates];
编辑 你的代码运行良好。我测试过了。这里

代码:

输出:

日志:

如果遗漏了什么,请检查上面的测试代码

我认为问题在于表的委托,请确保表中同时包含
UITableViewDataSource
UITableViewDelegate
的委托

希望这有帮助。。干杯……)

试试这个序列

NSMutableArray *indexPaths=[[NSMutableArray alloc]init];


-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{

    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    [tableView beginUpdates];

    RSDrug *drug = [[RSDrug alloc]init];
    [drugArray insertObject:drug atIndex:0];

    if (indexPath.row == 0) {
        [indexPaths removeAllObjects];

        [indexPaths addObject:[NSIndexPath indexPathForRow:indexPath.row++   inSection:0]];  // here inserting row index=1;

        [tableViewMenuList insertRowsAtIndexPaths:indexPaths withRowAnimation:UITableViewRowAnimationFade];
    }

    [tableView endUpdates];
}

我知道如果我写indexPathForRow:0它会工作,但这不是我需要的。默认情况下,我的tableview有一行,这一行用作按钮,我单击它,tableview添加一行。并始终将其添加到第2行,因此我必须使用indexPathForRow:1.Ahhh。。可以所以这个
[drugArray insertObject:drugatindex:0]
应该是
[drugArray-insertObject:druge-atIndex:1]
[nsindepath indexPathForRow:1第0节]
numberOfRowsInSection返回drugArray.count+1
默认情况下,drugArray是一个空数组,单击第一行时,它将添加一个对象。因此,指数为0;我认为索引是正确的,因为这些代码以前工作得很好,但我不知道为什么它对任何人都不起作用,从昨天到今天早上我没有做任何更改。@ghysrc,在那里检查一下……)嗯,我发现了那个愚蠢的错误,你可以从我上次的编辑中看到。对不起,浪费了你的时间。谢谢,还是不行。我真的不明白为什么它昨天起作用了。今天早上我打开我的电脑,再次运行,同样的代码,不再工作了。