Chandrika Visvesh
Chandrika Visvesh

Reputation: 91

CustomCell in UITableView in objective c

enter image description here

I have attached a sample output here .... It has two cells first one is having functioncode,starttime,endtime and second one has break time.

Now want i want to do is third one should contain functionCode:,starttime:,endtime:,

My array has 3 dictionary

(
        {
        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;
    } )

This is my cellforrow at indexpath method

 - (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;

    }

}

Upvotes: 0

Views: 305

Answers (2)

Shehzad Ali
Shehzad Ali

Reputation: 1845

Make a class named MyObject inherit from NSObject. It will contain the following properties date, end_time, function_code, operator_id, start_time, total_time, total_units.

Then you will make another class MyObject2 containing only one property ** break_time**.

You will parse your dictionary into respective models by adding a check if allkeys on dictionary is greater than 1 it means you have to parse your data in myObject1 otherwise you will parse it into myObject2. You will be adding these parsed objects in your array which can further be used as a datasource for uitableview. Now in delegate method of tableview cellForRowAtIndexPath, all you need is to implement a check.

 - (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;
}

On your existing code you can add a check.

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;
}

Upvotes: 2

Ricardo
Ricardo

Reputation: 2923

What is the question?

Probably you need to build 2 different cells, and depending on some data, you return one or another in cellForRow.

Upvotes: 0

Related Questions