【问题标题】:Add value in NSArray in Tableview in ios在 ios 的 Tableview 中的 NSArray 中添加值
【发布时间】:2017-05-10 23:40:43
【问题描述】:

这是我的代码,我正在尝试在 Array 中添加值并将它们填充到 Tableview 中。

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return [self.greekLetter count];
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    NSString *SimpleIdentifier = @"SimpleIdentifier";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:SimpleIdentifier];

    if(cell == nil){
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:SimpleIdentifier];
    }

    cell.textLabel.text = self.greekLetter[indexPath.row];

    return cell;
}

当我添加值群代码值时,它在表格视图中显示正确。

self.greekLetter=@[@"prem",@"nath",@"kumar",@"shree"];

我想为数组动态添加值,它将显示在表格视图中。有人可以帮助我吗?

【问题讨论】:

标签: ios objective-c tableview


【解决方案1】:

假设您的greekLetterNSMutableArray

-(void) addDataToGreekLetter {
    [self.greekLetter addObject:@"TestObject"];
    [self.tableView reloadData];
}

假设你的greekLetterNSArray

-(void) addDataToGreekLetter {
    NSMutableArray *temp = [NSMutableArray arrayWithArray: self.greekLetter];
    [temp addObject:@"TestObject"];
    self.greekLetter = [NSArray arrayWithArray:temp];
    [self.tableView reloadData];
}

【讨论】:

    【解决方案2】:

    您可以尝试只使用insertRowsAtIndexPaths,而不是重新加载整个UITableView。它不会重新加载所有单元格并在其上为您提供漂亮的动画。

    例如:

    -(void)addData:(NSString *)text{
    
        //provided your self.greekLetter is NSMutableArray 
    
        [self.greekLetter addObject:text]
    
        NSIndexPath *index = [NSIndexPath indexPathForRow:(self.greekLetter.count - 1) inSection:0];
    
        [self.tableview insertRowsAtIndexPaths:@[index] withRowAnimation:UITableViewRowAnimationRight];
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2013-05-18
      • 1970-01-01
      • 2015-08-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多