【问题标题】:Issue in adding dynamic cell in table view?在表格视图中添加动态单元格的问题?
【发布时间】:2012-02-10 13:38:57
【问题描述】:

请查看我用来在表格视图中添加动态单元格添加运行时间的代码。在选择我的表格视图时,我调用了这个方法。

 - (void) allServersFound 
{
    // called from delegate when bonjourservices has listings of machines:
    NSArray *newPosts = [[NSArray alloc]initWithObjects:@"A", nil]; // NSArray of machine names;

NSMutableArray *tempArray = [[NSMutableArray alloc] initWithObjects: @"A",@"B",@"C",@"D", nil];

int i = 0;
for (NSArray *count in newPosts)
{
    [tempArray addObject:[NSIndexPath indexPathForRow:i++ inSection:0]];
}

[[self tblHome] beginUpdates];
[[self tblHome] insertRowsAtIndexPaths:tempArray withRowAnimation:UITableViewRowAnimationNone];
[[self tblHome] endUpdates];

[tempArray release];
}

但这在运行时给了我以下异常: * 由于未捕获的异常“NSInvalidArgumentException”而终止应用程序,原因:'-[NSIndexPath _fastCStringContents:]:无法识别的选择器发送到实例 0x4e0e130

【问题讨论】:

    标签: iphone objective-c ios ipad


    【解决方案1】:

    首先你用这样的字符串初始化你的 tempArray

    NSMutableArray *tempArray = [[NSMutableArray alloc] initWithObjects: @"A",@"B",@"C",@"D", nil];
    

    然后你像这样添加 indexPaths:

    [tempArray addObject:[NSIndexPath indexPathForRow:i++ inSection:0]];
    

    因此,您传递给 insertRowsAtIndexPaths 方法的数组包含字符串和 indexPaths 对象。我认为这是异常的原因。

    【讨论】:

      【解决方案2】:

      正如其他人所说,您的 tempArray 包含 NSStringNSIndexPath 对象的混合。这是您在做任何其他事情之前需要解决的最明显的事情。您可以为此使用 [NSMutableArray array] 类方法(它是自动释放的,因此您需要在方法结束时删除对 [tempArray release] 的调用)。

      一个不太明显的事情是你的 UITableView 的 model必须在你调用 insertRowsAtIndexPaths 之前更新,否则你会在一个不太明显的地方得到另一个异常.

      【讨论】:

        【解决方案3】:

        您的tmparray 包含NSString 值以及IndexPaths,请先尝试删除NSStrings。 换行试试

        NSMutableArray *tempArray = [[NSMutableArray alloc] initWithObjects: @"A",@"B",@"C",@"D", nil];
        

        用线

        NSMutableArray *tempArray = [[NSMutableArray alloc] initWithObjects:nil];
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2021-11-18
          • 2019-07-18
          • 1970-01-01
          • 2018-02-26
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多