iOS表格单元格仅返回匹配数据的最后一行

时间:2012-07-26 06:44:38

标签: iphone ios dynamic uitableview

我正在尝试将数据插入到我创建的行中,但它只插入数据的最后一行。有人可以建议一种避免这种错误的方法吗?对于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;
  }
)

3 个答案:

答案 0 :(得分:0)

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

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

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

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

答案 1 :(得分:0)

你需要做这样的事情。

创建一个NSArray名称,例如mainArray,它包含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。

答案 2 :(得分:0)

由于我不知道如何继续使用我编写的这些杂乱的代码,我提出了另一个问题,让人们就如何动态插入部分和行提出建议,然后重做我的表部分的代码并且它完美运作! :)

链接位于:iOS Using NSDictionary to load data into section and rows

在此,我使用NSDictionary相应地动态插入数据到日期。 :)希望它可以帮助任何与我有同样问题的人!