iOS Multiple Columns in Uitableview

How to display multiple columns in a UITableView?

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

Make tableview with multiple columns

There are multiple ways of doing this. Here is how I would do it.
With respect to saving me much time I am going to skip a bunch of steps and ASSUME you know how to do each certain steps of setup.

Step 1: Establish a new ViewController with a connected cocoa touch class file.
NOTE: you can use a TableView Controller if you wish
Step 2: Drag in a tableView into your new view Controller and size it accordingly (also add basic constraints) Sample Image

Step 3: Add a new uiView into your viewController and name it header and place it at the top of the page.
NOTE: There are other ways of making a header such as inside the tableview itself. However I just prefer to do it this way.

Sample Image

Step 4: Connect your table view and header as outlets to the code.
Step 5: Create a new custom cell class within your new cocoa touch swift file. Make sure to set the delegate and datasource as like so:

import UIKit

class myCustomCell: UITableViewCell {

}


class test: UIViewController, UITableViewDelegate, UITableViewDataSource {

@IBOutlet weak var headerVIew: UIView!
@IBOutlet weak var myTableView: UITableView!

override func viewDidLoad() {
super.viewDidLoad()
self.myTableView.delegate = self
self.myTableView.dataSource = self
// Do any additional setup after loading the view.
}


/*
// MARK: - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
// Get the new view controller using segue.destination.
// Pass the selected object to the new view controller.
}
*/

}

NOTE: You can also CTRL Drag from your tableView to your VIEWCONTROLLER to connect the dataSource and delegate manually.

Step 6: Create your header view by adding in labels and sizing them accordingly.
I kinda rushed as you can see and they are not perfectly aligned.

Sample Image

Step7 : Add a single prototype cell to your table view using the attribute inspector. Size the cell as big or small as you want. And add 5 labels to the cell and match it to the size of the header you already created.
NOTE: You will need to do some customization to the labels, like providing a border or adding in other images to create the BOX Style you want.
Sample Image

Note that my labels are not perfectly aligned and are not sized perfectly.
Just wanted to save some time.

Step 8: THis is tricky! You need to select your UITableViewCell in your heirarchy and inside the identity inspector, set its custom class to whatever you named it in your file. In my case it is 'myCustomCell' Once this is done you are now able to connect them by CTRL dragging each object into the custom class TableViewCell as described in step 5.
It should look something like this,

import UIKit

class myCustomCell: UITableViewCell {

@IBOutlet weak var label1: UILabel!
@IBOutlet weak var label2: UILabel!
@IBOutlet weak var label3: UILabel!
@IBOutlet weak var label4: UILabel!
@IBOutlet weak var label5: UILabel!

}


class test: UIViewController, UITableViewDelegate, UITableViewDataSource {

@IBOutlet weak var headerVIew: UIView!
@IBOutlet weak var myTableView: UITableView!

override func viewDidLoad() {
super.viewDidLoad()
self.myTableView.delegate = self
self.myTableView.dataSource = self
// Do any additional setup after loading the view.
}

Finally you need to add the protocol functions in.

func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
//set up here
}

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
//set up here
}

That should be all you need to start creating this custom spreadsheet like view.
Most of the look and feel will come with more designing and proper setup with your labels and or UIimages. Not rushed like i did it .

Hope this helps

How to have multiple columns in tableview or alternative view?

They will most likely have used a UITableView with custom UITableView cells.

Using an XIB (nib) view to create the UI for a row and then passing the data in.

Check out here and here for more info.

Swift: Customizing TableView to hold multiple columns of data

Remove

self.tableView.registerClass(TableViewCell.self, forCellReuseIdentifier: "cell")

From viewDidLoad(), you don't need to register your UITableViewCell subclass if you're using prototype cells.

How do I create a UI TableView with multiple columns in Xcode?

iOS table views are always a single column. On Mac OS you can create what you are after directly.

That said, you could create custom table view cells that display the content you want. In fact it would be quite easy. All you would do is to subclass UITableViewCell and define views (probably UILabels) for each of you columns, then wire them up as outlet properties in the cell. Then rig your table view to register that cell class for the cell identifier you use.

You then write your cellForRowAtIndexPath method to install your data into the different outlet properties.

You could also use a UICollectionView, but it looks to me like a table view with custom cells is a better fit for this application.

Multiple columns to UITableView in iOS

UICollectionView is available in Available in iOS 6.0 and later and therefore cannot be used if you want to support earlier version.
And as per your requirement you need to display a GridView

So to support earlier versions and get other cool features you can use other libraries here are some of the options:

1) KKGridView

2) UIGridView

3) AQGridView

4) NRGridView

5) MMGridView

6) WCGridView

I am currently using AQGridView so surely i recommend that as it is the least buggy and its functions are very similar to UITableView.

Also that If you are trying to do this without XIB it will be little bit difficult for you to handle it but you can create a view controller with Xib file to Create the interface of your choice. Here is the Video of how it can be done in the best possible way by Evadne Wu. And here is the Sample Project

Stuck understanding how to create a table with multiple columns in iOS Swift

One approach is to use a custom cell in a tableviewcontroller. Your story board consists of a table in which the cell is a custom cell with UILabels for columns laid out next to each other (with properly defined constraints).

Example code for the controllers looks like:

import UIKit

class TableViewController: UITableViewController {

override func viewDidLoad() {
super.viewDidLoad()

}

override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
}

// MARK: - Table view data source

override func numberOfSectionsInTableView(tableView: UITableView) -> Int {
return 1
}

override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {

return 3
}


override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCellWithIdentifier("reuseIdentifier", forIndexPath: indexPath) as TableViewCell
cell.column1.text = "1" // fill in your value for column 1 (e.g. from an array)
cell.column2.text = "2" // fill in your value for column 2

return cell
}

}

and:

import UIKit

class TableViewCell: UITableViewCell {

@IBOutlet weak var column1: UILabel!
@IBOutlet weak var column2: UILabel!
override func awakeFromNib() {
super.awakeFromNib()
// Initialization code
}

override func setSelected(selected: Bool, animated: Bool) {
super.setSelected(selected, animated: animated)

// Configure the view for the selected state
}

}


Related Topics



Leave a reply



Submit