vicky vicky - 4 months ago 10
Objective-C Question

How to create fixed amount of UITableViewCell and if the data is null then row should be not be shown

I am using tableView to display some information which is just four line of information. And i want to assign respective information to each row.
Like how shown in the below image there are four rows, same as in the image so i am using tableView for that. Here my problem is that i have created four cells but don't know how should i use label in specific cell and show the info.
and also if the value is null that row should not be there means if two values among four are null then only two rows having values should be there in tableView. How can i achieve this. Till now i am only able to show one row information only.

enter image description here

- (NSArray *)myTableViewCells
{
if (!_myTableViewCells)
{
_myTableViewCells = @[
[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:nil],
[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:nil],
[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:nil],
[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:nil]

];
}

return _myTableViewCells;
}


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return self.myTableViewCells.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell* cell = self.myTableViewCells[indexPath.row];
NSManagedObject *managedObject = [self.devices lastObject];

cell.textLabel.text = [NSString stringWithFormat:@"%@",[managedObject valueForKey:@"personality_company_master_values"]];

//not getting have to do this way or any other way please help

// secondLabel.text = [NSString stringWithFormat:@"%@",[managedObject valueForKey:@"company_master_values"]];

// thirdLabel.text = [NSString stringWithFormat:@"%@",[managedObject valueForKey:@"tag_master_values"]];

return cell;
}

Answer

Get the value you want display in an array. Something like this

@property (monatomic, strong)NSMuatableArray *displayValues;


-(void)viewDidLoad
{

    self.displayValues =  [[NSMutableArray alloc]init];
    NSManagedObject *managedObject = [self.devices lastObject];

    if([managedObject valueForKey:@"personality_company_master_values"] != nil)
    {
    [self.displayValues addObject:[managedObject valueForKey:@"personality_company_master_values"]];
    }
    if([managedObject valueForKey:@"company_master_values"] != nil)
    {
    [self.displayValues addObject:[managedObject valueForKey:@"company_master_values"]];
    }

    if([managedObject valueForKey:@"tag_master_values"] != nil)
    {
    [self.displayValues addObject:[managedObject valueForKey:@"tag_master_values"]];
    }
}






          - (NSArray *)myTableViewCells
                 {
                 if (!_myTableViewCells)
                 {
                     _myTableViewCells = @[
                                          [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:nil],
                                          [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:nil],
                                          [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:nil],
                                          [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:nil]

                                          ];
                   }

                    return _myTableViewCells;
                   }


                 - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
                 {
                    return self.displayValues.count;
                  }

                     - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
                 {
                UITableViewCell* cell = self.myTableViewCells[indexPath.row];
                // NSManagedObject *managedObject = [self.devices lastObject];

                //cell.textLabel.text = [NSString stringWithFormat:@"%@",[managedObject valueForKey:@"personality_company_master_values"]];
                cell.textLabel.text = self.displayValues[indexPath.row];
                   //not getting have to do this way or any other way please help  

                   // secondLabel.text = [NSString stringWithFormat:@"%@",[managedObject valueForKey:@"company_master_values"]];

                   // thirdLabel.text = [NSString stringWithFormat:@"%@",[managedObject valueForKey:@"tag_master_values"]];        

                return cell;
                }
Comments