【问题标题】:Xcode UITableView sections and rows sorted by date (from mutableArray)Xcode UITableView 部分和按日期排序的行(来自 mutableArray)
【发布时间】:2015-03-31 14:33:15
【问题描述】:

在我的 UITableView 中,我有多个带有日期标题的部分。 日期在 NSMutableArray 中按日期排序。 我的部分显示正确的日期,工作正常,numberofrowsinnsection 也工作正常。 对于单元格文本标签,我得到了 NSMutableArrays,它们按照与排序日期相同的顺序排序。 如何获得相应日期(部分)的正确文本标签? 使用 indexpath.section 它仅在一个部分中有 1 行时才有效。当我在一个部分中有 2 行或更多行时,文本标签的值错误。 我知道这可以用字典来完成,但我找不到解决方案。

@property NSMutableArray *dateArray;     //date for my sections
@property NSMutableArray *hoursArray;   //hours for cells textlabel




我的部分

"January 2014",
"April 2014",
"June 2014"

我的时间:

"1",
"2",
"3",

//////////编辑

当我使用 Cell.textlabel.text =[hoursarray objectatindex:indexpath.row] 它总是从每个部分的索引 0 开始。

当我使用 Cell.textlabel.text =[hoursarray objectatindex:indexpath.section] 当每个部分只有 1 行时,它具有正确的排序。 但是当一个部分有更多行时,顺序是错误的

【问题讨论】:

  • 你能详细说明一下吗?
  • 看到编辑,是否可以理解?

标签: ios sorting tableview rows sections


【解决方案1】:

您必须创建排序的数据源,因此只需创建 rowArray 的 sectionArray,然后相应地使用它。 然后你可以这样使用它:

Cell.textlabel.text = [[sectionArray objectatindex:indexpath.section] objectAtIndex:indexpath.row];

现在您只需要相应地管理您的数据源。

更新: 你可以像这样创建二维数组

NSMutableArray *dateArray = [[NSMutableArray alloc] init];


for(int i=0; i<=dateData.length; i++)
{
   NSMutableArray *hoursArray = [[NSMutableArray alloc] init];

   for(int j=0; j<=hoursData.length; j++)
   {
     [hoursArray addObject:yourObject];
   }

   [dateArray addObject:hoursArray];
}

这只是一个示例,但您可以根据需要进行优化。

【讨论】:

  • 我使用 sortedArrayUsingSelector:@selector(compare:)] 对日期数组进行了排序;之后,我按日期数组的顺序对 hoursarray 进行排序...但是何时使用您的代码 [[sectionArray objectatindex:indexpath.section] objectAtIndex:indexpath.row];应用程序因错误而崩溃:-[__NSCFString objectAtIndex:]: unrecognized selector sent to instance。我需要一个二维数组吗?
  • @user2912794 是的,这就是我提到的二维数组.. dateArray 包含 hoursArray..
  • 你能给我看一个二维数组应该是什么样子的例子吗?
  • 我想我明白了,每个部分都需要包含其数据的自己的数组,我将此数组添加到“主”数组中!?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-09-28
  • 1970-01-01
  • 1970-01-01
  • 2011-02-19
  • 2023-03-27
  • 1970-01-01
相关资源
最近更新 更多