Bittu
Bittu

Reputation: 371

Set Multiple UILabel in a row of UITable

I want to set UILabel in row of table, but the issue is that the number of label each time is different when the app restarts. Clear that more detail that I want to create a table which has dynamic row and column. Rows are set themselves, but in the column i set the label which is also dynamics, so how can i set that label each time of new web service will call ?

Ex.

row/column

A1 b11 b12 .......b1n

A2 b21 b22 .......b2n

A3 b31 b32 .......b3n . .

Am bm1 bm2 .......bmn

where A is row or b is column

Upvotes: 1

Views: 1236

Answers (5)

Tariq
Tariq

Reputation: 9979

Better option would be just create a UITableView in UITableViewCell and your each label would be cell and you can disable inner tableview scrolling. But you need to make sure that you have calculated inner tableview height properly.

Other solution like adding n number of UIlabel etc wont give you smooth scrolling.

Upvotes: 1

andreamazz
andreamazz

Reputation: 4286

You should consider using a different view, such as DTGridView

Upvotes: 0

Prasad G
Prasad G

Reputation: 6718

You use tag method and indexpath.row.

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

            UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];

            if (!cell) {
                      cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"cell"];
                      for (i = 0; i < n; i++) {
                              label = [[UILabel alloc] initWithFrame:CGRectMake (i * yourWidth, 0, yourWidth + 5, yourHight)]; 
                              label.textColor = [UIColor blueColor];
                              label.tag = (indexPath.row)*100 + i  //you can create n labels in a row.
                              label.text = [NSString stringWithString:@"%d",label.tag]; 
                              [cell.contentView addSubview:label];
                        }
            }

        return cell;

} 

I think it will be helpful to you.

Upvotes: 0

jhoanna
jhoanna

Reputation: 1857

You could have a mutable array called rowData for example. In your init/initWithCoder/etc method, you could create another mutable array called columnData for example that could contain the data for each label. Add a columnData object for each row to rowData.

For your cellForRowAtIndexPath method, you could do something like this:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];

if (!cell) {
    cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"cell"] autorelease];

    int row = [indexPath row];
    int i = 0;
    int nLabels = [[rowData objectAtIndex:row] count];
    UILabel *label;

    NSLog([NSString stringWithFormat:@"%d labels", nLabels]);

    for (i = 0; i < nLabels; i++) {
        NSLog([NSString stringWithFormat:@"%d", i]);
        label = [[[UILabel alloc] initWithFrame:CGRectMake(cell.frame.origin.x + (i * 40), cell.frame.origin.y, 40, cell.frame.size.height)] autorelease]; //replace 40 with desired label width
        label.textColor = [UIColor grayColor];
        label.backgroundColor = [UIColor clearColor];
        // set the label text here

        [cell.contentView addSubview:label];

        NSLog([NSString stringWithFormat:@"%d subviews", [[cell.contentView subviews] count]]);
    }
}

return cell;

}

Upvotes: 2

David Hoerl
David Hoerl

Reputation: 41632

Create cells that have the maximum number of labels you might need. Make their tag be 1,2,3,4... - increasing numbers. Each time you are asked to supply a cell by the table, you either take an old one or new one, decide how many labels you need, make that range of labels visible (ie label.hidden = NO;), then hide the other labels. You will need to change the text of the visible labels obviously.

Upvotes: 0

Related Questions