0

我正在尝试将数据插入到我创建的行中,但它只插入数据的最后一行。任何人都可以提出一种避免此错误的方法吗?对于cellForRowAtIndexPath我的代码是:

NSInteger counter = [myArray count];
if (myArray.count > 0){

    for (int i = indexPath.section; i<indexPath.section+1; i++){
        for(int a = 0 ; a < counter; a++) {
            NSDictionary *dic = [counterArray objectAtIndex:i];
            NSString *dateString = [dic valueForKey:@"date"];

            NSDictionary *dic2 = [myArray objectAtIndex:a];
            NSString *dateString2 = [dic2 valueForKey:@"date"];

            if ([dateString isEqualToString:dateString2]){
                cell.bookTitle.text = [dic2 objectForKey:@"name"];
            }
        }
    }   
}
return cell;

}

到目前为止,它看起来像这样:

数组实际上是这样的:

(
  {
    BIBNo = 187883;
    date = "13/08/2012";
    itemSequence = 000010;
    name = "Positive thinking / Susan Quilliam.";
    noOfRenewal = 2;
    number = 000187899;
    status = "Normal Loan";
    time = "24:00";
    type = Loans;
  },
  {
    BIBNo = 161816;
    date = "14/08/2012";
    itemSequence = 000010;
    name = "Malaysian Q & As / compiled by Survey & Interview Department ; illustrations by Exodus.";
    noOfRenewal = 0;
    number = 000161817;
    status = "Normal Loan";
    time = "24:00";
    type = Loans;
  },
  {
    BIBNo = 187882;
    date = "14/08/2012";
    itemSequence = 000010;
    name = "Increasing confidence / Philippa Davies.";
    noOfRenewal = 0;
    number = 000187907;
    status = "Normal Loan";
    time = "24:00";
    type = Loans;
  },
  {
    BIBNo = 291054;
    date = "14/08/2012";
    itemSequence = 000010;
    name = "iPhone application development for IOS 4 / Duncan Campbell.";
    noOfRenewal = 2;
    number = 000291054;
    status = "Normal Loan";
    time = "24:00";
    type = Loans;
  },
  {
    BIBNo = 244441;
    date = "15/08/2012";
    itemSequence = 000010;
    name = "Coach : lessons on the game of life / Michael Lewis.";
    noOfRenewal = 1;
    number = 000244441;
    status = "Normal Loan";
    time = "24:00";
    type = Loans;
  },
  {
    BIBNo = 290408;
    date = "15/08/2012";
    itemSequence = 000010;
    name = "Sams teach yourself iPhone application development in 24 hours / John Ray.";
    noOfRenewal = 3;
    number = 000290408;
    status = "Normal Loan";
    time = "24:00";
    type = Loans;
  }
)
4

3 回答 3

0

我之前也遇到过此类问题,这是由于单元格标识符:由于所有单元格都相同,因此它将使单个单元格出列并将其用于所有行。我最终通过一种非常hacky且可能不理想的方法解决了这个问题..但它确实有效。

我像这样实现单元格标识符:

NSString *CellIdentifier = [NSString stringWithFormat:@"cell-%d-%d", indexPath.section, indexPath.row];

这保证了任何被重用的给定单元格都用于其特定的部分/行。

于 2012-07-26T08:56:35.607 回答
0

你需要做这样的事情。

创建一个类似 mainArray 的 NSArray 名称,其中包含 Nsarray 对象(假设这些用于部分)并且这些部分数组将具有 NSDictionary 对象(假设这些是每个部分的行)

然后你可以像这样运行循环

for(unsigned int counter = 0;counter<[mainArray count];counter++){
  NSArray *sectionArray = [mainArray objectAtIndex:indexPath.section];
 for(unsigned int counter1 = 0;counter1 < [sectionArray count];counter++ ){
   NSDictionary *mainDataDictionary = [sectionArray objectAtIndex:indexPath.row];
  //Write your code here for a particular cell/row.
 }

}

您还需要根据此修改 numberOfRowsInSection。

于 2012-07-26T09:25:28.663 回答
0

由于我不知道如何继续编写我编写的这些乱七八糟的代码,我提出了另一个问题,让人们就如何动态插入部分和行提供建议,并且我为我的表格部分重新编写了代码,它工作得很好!:)

链接在这里:iOS Using NSDictionary to load data into section and rows

在此,我使用 NSDictionary 根据日期动态插入数据。:) 希望它能帮助和我有同样问题的人!

于 2012-07-27T05:59:16.547 回答