bubibu
bubibu

Reputation: 324

Objective c different uilabel in a row of a table cell

I am trying to create a table that in each row, it contains some uilabels. The number of the uilabel is based on my array, if my array has one string then it would only be one label, if my array has 2 strings then the row will have two labels.

First Row:

enter image description here

Second Row:

enter image description here

Here's my code to put in the strings into the uilabels that are already there. How to make the cell to auto generate somemore uilabels if I have more strings?

- (void)updateCell:(NSString *)text1 label2:(NSString *)text2 label3:(NSString *)text3{
   self.testLabel1.text = text1;
   self.testLabel2.text = text2;
   self.testLabel3.text = text3;
}

- (void)createLabel{
   //create uilabels based on the size of array?
}

If this is not workable, what might be some alternative ways? Any advice is much appreciated.

Upvotes: 2

Views: 950

Answers (3)

Rikh
Rikh

Reputation: 4222

I would recommend using a UICollectionView with the data source of the UICollectionView be an array that will hold the number of strings in that particular row. Each cell of the UICollectionView will hold one label. I personally find this way easier to manage.

Upvotes: 0

yawnobleix
yawnobleix

Reputation: 1342

This should all be done in cellForRowAtIndexPath, you should basically use your array for that row index and then create the uilabels programmatically, here is some code, it won't work but the logic should be okay

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

NSArray *stringLabels = [self.labels objectAtIndex:indexPath.row]; // im not sure how you are getting the array

    for (NSString *str in stringLabels) {
        UILabel *newLabel = [[UILabel alloc]init];
        newlabel.SetFrame = CGrectMake(Set to your desired layout);
        newlabel.text = str;
        // uilabel formatting

        [cell addSubview:newLabel]
    }

}

This approach is very flexible as it allows you to have any number of labels in every cell.

Upvotes: 1

KKRocks
KKRocks

Reputation: 8322

Try this

You need to create different cell as per your condition and configure CellForAtIndexPath as below :

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    NSString *strFirstIndentifier = @"FirstIndentifier"; // set this idenfier to UITableViewCell in StroryBoard for all
    NSString *strSecondIndentifier = @"SecondIndentifier";
    NSString *strThirdIndentifier = @"ThirdIndentifier";
    UITableViewCell *adjustcell ;
    if (arrUnits.count == 1) {
        adjustcell = [tableView dequeueReusableCellWithIdentifier:strFirstIndentifier];
        //configure your lables here
    }
    else if (arrUnits.count == 2) {
        adjustcell = [tableView dequeueReusableCellWithIdentifier:strSecondIndentifier];
        //configure your lables here
    }
    else if (arrUnits.count == 3) {
        adjustcell = [tableView dequeueReusableCellWithIdentifier:strThirdIndentifier];
        //configure your lables here
    }

    return adjustcell;
}

Upvotes: 0

Related Questions