objective c 中 UITableView 中的 CustomCell

CustomCell in UITableView in objective c

我在这里附上了一个样本输出....它有两个单元格,第一个有功能代码、开始时间、结束时间,第二个有休息时间。

现在我想做的是第三个应该包含 functionCode:,starttime:,endtime:,

我的数组有 3 个字典

(
        {
        date = "2016-01-20";
        "end_time" = "11:10:00";
        "function_code" = RCV;
        "operator_id" = JOHN;
        "start_time" = "11:00:00";
        "total_time" = 10;
        "total_units" = 19;
    },
        {
        "break_time" = 65;
    },
        {
        date = "2016-01-20";
        "end_time" = "12:25:00";
        "function_code" = PIK;
        "operator_id" = JOHN;
        "start_time" = "12:15:00";
        "total_time" = 10;
        "total_units" = 26;
    } )

这是我在 indexpath 方法中的 cellforrow

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

   if (indexPath.row ==0)
    {

        self.tempDictionary = [self.arrData1 objectAtIndex:indexPath.row];
        NSLog(@"The dictionary contains:%@",self.tempDictionary);


        static NSString *CellIndentifier =@"TableViewCell";
        TableViewCell *cell =[tableView dequeueReusableCellWithIdentifier:CellIndentifier];

        if (cell == nil) {
            cell = [[TableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIndentifier];

       }

        NSLog(@" the dictionary contains all :%@",self.tempDictionary);


        NSString *strFunctionCodeValue =[self.tempDictionary objectForKey:@"function_code"];
        NSString *strStartTimeValue = [self.tempDictionary objectForKey:@"start_time"];
        NSString *strEndTimeValue = [self.tempDictionary objectForKey:@"end_time"];
        NSString *strTotalUnitsValue =[self.tempDictionary objectForKey:@"total_units"];
        NSString *strTotalTimeValue = [self.tempDictionary objectForKey:@"total_time"];


        cell.lblFunctionCodeValue.text = strFunctionCodeValue;
        cell.lblStartTimeValue.text = strStartTimeValue;
        cell.lblEndTimeValue.text = strEndTimeValue;
        cell.lblTotalUnitsValue.text =strTotalUnitsValue;
        cell.lblTotalTimeValue.text = strTotalTimeValue;

        return cell;

    }
    else
    {

        static NSString *cellIdentifier1 =@"TableViewCell1";
        self.tempDictionary = [self.arrData1 objectAtIndex:indexPath.row];
        NSLog(@" dicitionary :%@",self.tempDictionary);

        TableViewCell1 *cell1 =(TableViewCell1 *)[tableView dequeueReusableCellWithIdentifier:cellIdentifier1];
        if (cell1 == nil) {
            cell1 =[[TableViewCell1 alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier1];

      }

        NSString *breakTimeValue = [self.tempDictionary objectForKey:@"break_time"];
        cell1.lblBreakTimeValue.text = breakTimeValue;

        return cell1;

    }

}

问题是什么?

可能您需要构建 2 个不同的单元格,并且根据一些数据,您 return 在 cellForRow 中一个或另一个。

创建一个名为 MyObject 的 class 从 NSObject 继承。它将包含以下属性 日期,end_time、function_code、operator_id、start_time、total_time、total_units。

然后您将创建另一个 class MyObject2,其中仅包含一个 属性 ** break_time**。

您将通过添加检查来将字典解析为相应的模型,如果字典上的所有键都大于 1,这意味着您必须解析 myObject1 中的数据,否则您会将其解析为 myObject2。您将在数组中添加这些已解析的对象,这些对象可以进一步用作 uitableview 的数据源。现在在 tableview cellForRowAtIndexPath 的委托方法中,您只需要执行一个检查。

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

     id *object = [array objectAtIndex:indexPath.row];
     if([object isKindOfClass:MyObject]){
         CustomCell1 *cell1 = (CustomCell1 *)[tableView dequeueReusableCellWithIdentifier:@"CustomCell1"];
         return cell1;
     }else{
        CustomCell2 *cell2 = (CustomCell2 *)[tableView dequeueReusableCellWithIdentifier:@"CustomCell2"];
         return cell2;
     }
     return;
}

您可以在现有代码上添加检查。

self.tempDictionary = [self.arrData1 objectAtIndex:indexPath.row];
if([[self.tempDictionary allKeys] count]>1){
         static NSString *CellIndentifier =@"TableViewCell";
    TableViewCell *cell =[tableView dequeueReusableCellWithIdentifier:CellIndentifier];

    if (cell == nil) {
        cell = [[TableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIndentifier];

    }

    NSLog(@" the dictionary contains all :%@",self.tempDictionary);

    NSString *strFunctionCodeValue =[self.tempDictionary objectForKey:@"function_code"];
    NSString *strStartTimeValue = [self.tempDictionary objectForKey:@"start_time"];
    NSString *strEndTimeValue = [self.tempDictionary objectForKey:@"end_time"];
    NSString *strTotalUnitsValue =[self.tempDictionary objectForKey:@"total_units"];
    NSString *strTotalTimeValue = [self.tempDictionary objectForKey:@"total_time"];


    cell.lblFunctionCodeValue.text = strFunctionCodeValue;
    cell.lblStartTimeValue.text = strStartTimeValue;
    cell.lblEndTimeValue.text = strEndTimeValue;
    cell.lblTotalUnitsValue.text =strTotalUnitsValue;
    cell.lblTotalTimeValue.text = strTotalTimeValue;


}else{
    static NSString *cellIdentifier1 =@"TableViewCell1";
    self.tempDictionary = [self.arrData1 objectAtIndex:indexPath.row];
    NSLog(@" dicitionary :%@",self.tempDictionary);

    TableViewCell1 *cell1 =(TableViewCell1 *)[tableView dequeueReusableCellWithIdentifier:cellIdentifier1];
    if (cell1 == nil) {
        cell1 =[[TableViewCell1 alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier1];

 }


    NSString *breakTimeValue = [self.tempDictionary objectForKey:@"break_time"];

    cell1.lblBreakTimeValue.text = breakTimeValue;


    return cell1;
}