简体   繁体   English

Obj-C-点击单元格时向TableView添加新行?

[英]Obj-C- Add new row to TableView when cell is tapped?

I've got a tableview that allows users to add an item (a row) to an invoice (the tableview) when an existing row is tapped.我有一个表格视图,允许用户在点击现有行时将项目(一行)添加到发票(表格视图)中。 That said, I can't seem to add an empty row because my code is trying to set the information in the cell with data from my specified array, but naturally, the count in the array is different from my data source (as I want the count to be +1).也就是说,我似乎无法添加一个空行,因为我的代码正在尝试使用我指定数组中的数据设置单元格中的信息,但自然地,数组中的计数与我的数据源不同(如我所愿计数为+1)。

Eg I want to return 3 cells even if there are only 2 dictionaries in my array, and the third cell should be empty.例如,即使我的数组中只有 2 个字典,我也想返回 3 个单元格,并且第三个单元格应该为空。

I want this because the third cell allows my user to fill out empty fields, while the fields in the previous two rows are populated with their already input data.我想要这个,因为第三个单元格允许我的用户填写空字段,而前两行中的字段填充了他们已经输入的数据。 Here's how I'm trying to return the extra row right now, but as mentioned above, it crashes my app due to the imbalance of dictionaries returned in my array.这是我现在尝试返回额外行的方式,但如上所述,由于数组中返回的字典不平衡,它会使我的应用程序崩溃。

Here's my code so far:到目前为止,这是我的代码:

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
   
    self.allItems = [[NSMutableArray alloc] init];
    self.itemDetails = [[NSMutableDictionary alloc] init];

}

//TableView delegates
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {

    return 1;
   
    
}




-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {


    return self.allItems.count + 1;

}



-(UITableViewCell *)tableView:(UITableView*)tableView cellForRowAtIndexPath:(nonnull NSIndexPath *)indexPath {
    

        static NSString *ClientTableIdentifier = @"InvoiceDetailsTableViewCell";
        
       InvoiceDetailsTableViewCell *cell = (InvoiceDetailsTableViewCell *)[self.tableView dequeueReusableCellWithIdentifier:ClientTableIdentifier];
        
        if (cell == nil)
        {
            NSArray *nib = [[NSBundle mainBundle] loadNibNamed:@"InvoiceDetailsTableViewCell" owner:self options:nil];
            cell = [nib objectAtIndex:0];
            
        }
    
    if (self.allItems.count == 0) {
        
    } else {
        
        cell.itemName.text = [self.allItems valueForKey:@"Item Name"][indexPath.row];
     
        
    }
    
        return cell;
        
    
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
   InvoiceDetailsTableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
   
    NSString *itemTitle = cell.itemName.text;
    NSString *itemDescrip = cell.itemDescrip.text;
    NSString *itemCost = cell.itemCost.text;
    NSString *itemTax = cell.itemTax.text;
    
    
    [self.itemDetails setValue:itemTitle forKey:@"Item Name"];

    [self.itemDetails setValue:itemDescrip forKey:@"Item Description"];
    
    [self.itemDetails setValue:itemCost forKey:@"Item Cost"];
  
    [self.itemDetails setValue:itemTax forKey:@"Item Tax Rate"];

    [self.allItems addObject:self.itemDetails];
    
    [self.tableView reloadData];

}

One significant problem is the line that says:一个重要的问题是这样一行:

cell.itemName.text = [self.allItems valueForKey:@"Item Name"][indexPath.row];

Since your row count exceeds the number of items in your array, you will want to check the row number before accessing the array:由于您的行数超过了数组中的项目数,因此您需要在访问数组之前检查行号:

NSInteger row = indexPath.row;
if (row < self.allItems.count) {
    cell.itemName.text = self.allItems[row][@"Item Name"]; // personally, I’d get row first, and then keyed value second
} else {
    cell.itemName.text = @"";
}

You want to check to make sure that the current row is not the last (blank) row.您要检查以确保当前行不是最后(空白)行。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM