iOS 7 - TableView无法正确显示值

时间:2014-02-25 13:34:03

标签: ios objective-c uitableview

抱歉,我已经在这个问题上停留了很长一段时间,我无法弄清楚原因。我有一个有2个TableView的ViewController,我花了一些时间来管理这些功能,但它现在运行良好。最初,我有一些静态字符串作为表格单元格的显示,只是为了检查我是否可以让它运行。在管理完之后,我使用我的数组作为表格内容的来源。第一个表填充得很好,但是第二个表显示空白。

cellForRowAtIndexPath函数:

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

    UITableViewCell *priceCell = (UITableViewCell *)[tableView dequeueReusableCellWithIdentifier:PriceCellIdentifier];
    UITableViewCell *discountCell = (UITableViewCell *)[tableView dequeueReusableCellWithIdentifier:DiscountCellIdentifier];

    if(tableView == self.priceTable){
        [priceCell.textLabel setText:[NSString stringWithString:[priceArray objectAtIndex:indexPath.row]]];
        return priceCell;
    }
    else /*if(tableView == self.discountTable)*/ {
        [discountCell.textLabel setText:[NSString stringWithString:[discountArray objectAtIndex:indexPath.row]]];

        return discountCell;
    }

}

这是相当令人困惑的,因为我没有做任何事情,只是改变了我获得表格单元格所显示的值的位置。我还检查了我的discountArray并且它不是空的,我从这个函数中注销了值,我可以看到所说的值。

修改

这是我的viewDidLoad函数:

- (void)viewDidLoad{
    [super viewDidLoad];

    priceArray = [[NSMutableArray alloc] init];
    discountArray = [[NSMutableArray alloc] init];
    NSLog(@"received info %@", [self.selectedItem description]);
    [self executeSQL];

}

executeSQL是初始化priceArraydiscountArray

的函数
- (void) executeSQL{
  // Getting the database path.
  NSArray  *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
  NSString *docsPath = [paths objectAtIndex:0];
  NSString *dbPath = [docsPath stringByAppendingPathComponent:@"itemList.db"];

  FMDatabase *database = [FMDatabase databaseWithPath:dbPath];
  [database open];

  NSMutableString *sqlQuery = [NSMutableString stringWithString:@"SELECT ITEMNAME, BRAND, GROUPID, CLASS, UOM, AMT, AMT2, AMT3, DISC, DISC2, DISC3 FROM ItemList WHERE ITEMNAME = '"];

  [sqlQuery appendString:selectedItem];
  [sqlQuery appendString:@"'"];


  // Query result
  FMResultSet *resultsWithNameLocation = [database executeQuery:sqlQuery];

  [priceArray removeAllObjects];
  [discountArray removeAllObjects];

  //we get the results here.
  while([resultsWithNameLocation next]) {

      //for the AMT, AMT2, and AMT3
      if([[NSString stringWithFormat:@"%@",[resultsWithNameLocation stringForColumn:@"AMT"]] isEqualToString:@"(null)"]){
          [priceArray addObject:@""];
      }
      else{
          [priceArray addObject:[resultsWithNameLocation stringForColumn:@"AMT"]];
      }

      if([[NSString stringWithFormat:@"%@",[resultsWithNameLocation stringForColumn:@"AMT2"]] isEqualToString:@"(null)"]){
          [priceArray addObject:@""];
      }
      else{
          [priceArray addObject:[resultsWithNameLocation stringForColumn:@"AMT2"]];
      }

      if([[NSString stringWithFormat:@"%@",[resultsWithNameLocation stringForColumn:@"AMT3"]] isEqualToString:@"(null)"]){
          [priceArray addObject:@""];
      }
      else{
          [priceArray addObject:[resultsWithNameLocation stringForColumn:@"AMT3"]];
      }
      //end of priceArray

      //for DISC, DISC2, and DISC3

      if([[NSString stringWithFormat:@"%@",[resultsWithNameLocation stringForColumn:@"DISC"]] isEqualToString:@"(null)"]){
          [discountArray addObject:@""];
      }
      else{
          [discountArray addObject:[resultsWithNameLocation stringForColumn:@"DISC"]];
      }

      if([[NSString stringWithFormat:@"%@",[resultsWithNameLocation stringForColumn:@"DISC2"]] isEqualToString:@"(null)"]){
          [discountArray addObject:@""];
      }
      else{
          [discountArray addObject:[resultsWithNameLocation stringForColumn:@"DISC2"]];
      }

      if([[NSString stringWithFormat:@"%@",[resultsWithNameLocation stringForColumn:@"DISC3"]] isEqualToString:@"(null)"]){
          [discountArray addObject:@""];
      }
      else{
          [discountArray addObject:[resultsWithNameLocation stringForColumn:@"DISC3"]];
      }

      NSLog(@"DISC = %@", discountArray[0]);
      NSLog(@"DISC2 = %@", discountArray[1]);
      NSLog(@"DISC3 = %@", discountArray[2]);

  }
  [database close];

}

executeSQL函数的日志语句首先在cellForRowAtIndexPath函数之前执行,它告诉我我的数组有内容。它们不只是正确显示。

我很困惑,因为如果我将[discountCell.textLabel setText:[NSString stringWithString:[discountArray objectAtIndex:indexPath.row]]];替换为[discountCell.textLabel setText:[NSString stringWithFormat:@"Sample Text"]];,那么它就可以了。

有人可以帮我吗?谢谢。

1 个答案:

答案 0 :(得分:2)

您可能错过了为第二个UITableView设置委托。如果您还没有这样做,请在viewDidLoad

中进行设置

仔细检查您的故事板,将您的tableviews设置为动态。您是否在tableView中返回正确的行数:numberOfRowsInSection:

我希望这有帮助!