insertRowsAtIndexPaths或reloadData没有响应

时间:2015-06-27 02:32:51

标签: ios uitableview

这是我的代码。昨天它运作得很好。但是今天早上它没有用。

func willAnimateRotation() -> String {
    return "abc"
}

我尝试添加一些代码,比如beginUpdates / endUpdates,或者我删除了insertRowsAtIndexPaths,只需使用reloadData。

但同样的结果。当应用程序运行到insertRowsAtIndexPaths或reloadData时,它会卡住,没有响应。

所以我需要一些帮助。怎么解决这个问题?

发现问题..........一个愚蠢的错误

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

-(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;
}

我永远不知道我不能重复使用leftView。如果我将leftImage设置为field1,我就不能再将它设置为field2。

当我改为这个时,它再次起作用。

UIImageView *leftImage = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"img"]];
field_1.leftView = leftImage;
field_2.leftView = leftImage;

2 个答案:

答案 0 :(得分:0)

Have you tried this?

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

EDIT Your code are working fine. i tested it. here..

Code:

enter image description here

Output:

enter image description here

Log:

enter image description here

Check the tested code above if you have omitted something.

I think the problem is the delegates of your table, make sure that your table have the delegate for both UITableViewDataSource & UITableViewDelegate.

Hope this is helpful.. Cheers.. :)

答案 1 :(得分:0)

试试这个序列...

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