Annpurna Mishra
Annpurna Mishra

Reputation: 11

How to display multiple columns in a UITableView?

I want to display multiple columns in a UITableView.

For Example:

TableView

   FName        LName    Age

   -----        -----    ---

   Abby         Michale   34   

Upvotes: 16

Views: 60416

Answers (6)

riaz
riaz

Reputation: 1

add three labels as sub viewsinto uitableview cell's content.then assign apprrpriate values to it eg:

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

static NSString *CellIdentifier = @"Cell";

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
    cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier] autorelease];


}

//frame should be set accordingly(means should be framed accordingly).

UILabel *l1=[[UILabel alloc]initWithFrame:CGRectMake(5, 5, 100,100)];

UILabel *l2=[[UILabel alloc]initWithFrame:CGRectMake(110,10,100,80)];
UILabel *l3=[[UILabel alloc]initWithFrame:CGRectMake(115,60,100,50)];

[cell.contentView addSubview:l1];
[cell.contentView addSubview:l2];
[cell.contentView addSubview:l3];
 return cell;
 }

Upvotes: 0

flexicious.com
flexicious.com

Reputation: 1671

Bit late to the party, but we've open sourced our fully featured table component:

https://github.com/flexicious/iOSDataGrid

Some screenshots:

http://www.ioscomponents.com/Home/IOSDataGrid

Feel free to use!

Upvotes: 1

turkenh
turkenh

Reputation: 2584

I think the correct way of doing this is UICollectionView. Started with UITableView and finally failed at some point due to unexpected behavior while trying to implement scrolling both left-right and up-down.

Please check this awesome post for a complete and up-to-date solution: http://www.brightec.co.uk/blog/uicollectionview-using-horizontal-and-vertical-scrolling-sticky-rows-and-columns

Result will be like this:

enter image description here

Upvotes: 24

Tanin
Tanin

Reputation: 1923

I have created UIGridView. I believe your problem can be solved using the same technique.

You can learn the source code of UIGridView. The code is really short.

Upvotes: 3

Nava Carmon
Nava Carmon

Reputation: 4533

You can define a custom cell in IB, which will contain 3 labels and in function:

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

         static NSString *DetailCellIdentifier = @"DetailCell";

         UITableViewCell *cell = [aTableView dequeueReusableCellWithIdentifier:DetailCellIdentifier];

         if (cell == nil) {
            NSArray *cellObjects = [[NSBundle mainBundle] loadNibNamed:@"DetailCell" owner:self options:nil];
            cell = (UITableViewCell*) [cellObjects objectAtIndex:0];
        }

        // setup your cell
     }

when you define the cell in IB give each label a tag, so when you need to put a text there you can retrieve it by tag like this:

        label = (UILabel *)[cell viewWithTag:NAME_TAG];
        label.text = myObject.name; 

and repeat this with other 2 labels. The tag is a unique number.

Put this code instead //setup your cell comment

Upvotes: 14

Alex Reynolds
Alex Reynolds

Reputation: 96927

Create a custom UIView subclass containing, say, three UILabel elements as subviews. Then set the cell's contentView property to this custom view.

Upvotes: 0

Related Questions