0

I want to make a interface like setting Setting I think I need use uitableview. I want to use uitableview in uiviewcontroller. but I do not know how to do it...

I have tried to add a grouped uitableview in a uiviewcontroller, but the grouped uitableview does not show in the uiviewcontroller when i run application, i think i am far to do this..

Which type of uitablview i should use ? grouped ? static cell?

Could you tell me how to do it ? thank you. Do you have some tutorials or open source to study? Thank you.

user2262304
  • 329
  • 1
  • 3
  • 10

2 Answers2

1

You should use a UITableView with sections added. There is a pretty good tutorial here.

Updated

First, create a UITableView by dragging on onto your view controller. Add the necessary constraints, and generate a prototype cell.

enter image description here

Set the cell reuse identifier as cell.

enter image description here

Then, Control Left Click and drag the tableView to the ViewController (the yellow circle on the top). Do this twice and assign it as the DataSource and Delegate.

Open the assistant editor and control drag the tableView to the class to create an IBOutlet. Then add UITableViewDelegate to your class declaration:

class ViewController: UIViewController, UITableViewDelegate {

Once you have done this, create two new blank Swift files. File, New, File.

enter image description here

Title one file Section.swift and the other SectionsData.swift.

In the file Section.swift, add this code.

struct Section 
{
var heading : String
var items : [String]

init(title: String, objects : [String]) {

    heading = title
    items = objects
}
}

Here you are defining a structure so the data can be obtained later.

In the SectionsData file put in the following code. This is where you will be able to edit what goes into your table.

class SectionsData {

func getSectionsFromData() -> [Section] {


    var sectionsArray = [Section]()

    let hello = Section(title: "Hello", objects: ["Create", "This", "To", "The"])
    let world = Section(title: "World", objects: ["Extent", "Needed", "To", "Supply", "Your", "Data"])
    let swift = Section(title: "Swift", objects: ["Swift", "Swift", "Swift", "Swift"])


    sectionsArray.append(hello)
    sectionsArray.append(world)
    sectionsArray.append(swift)

    return sectionsArray
}
}

In this file, you created a class and then a function in order to save and retrieve the data.

Now, in the file with the IBOutlet of your tableview, create the follow variable.

var sections: [Section] = SectionsData().getSectionsFromData()

Now that the hard work is done, time to populate the table. The following functions allow for that.

func numberOfSectionsInTableView(tableView: UITableView) -> Int
{
    return sections.count
}

func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int
{
    return sections[section].items.count
}

func tableView(tableView: UITableView, titleForHeaderInSection section: Int) -> String?
{
    return sections[section].heading
}

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell
{
    let cell = tableView.dequeueReusableCellWithIdentifier("cell", forIndexPath: indexPath)

    cell.textLabel?.text = sections[indexPath.section].items[indexPath.row]

    return cell
}

You should be able to run this and get the results you want. You can edit the cell look when you supply the data. For example,

cell.textLabel?.font = UIFont(name: "Times New Roman", size: 30)

Just make sure when changing the font like that, the string name is exactly how it is spelled.

I hope this helps.

Matthew Bradshaw
  • 1,843
  • 3
  • 13
  • 21
  • 1
    While this link may answer the question, it is better to include the essential parts of the answer here and provide the link for reference. Link-only answers can become invalid if the linked page changes. - [From Review](/review/low-quality-posts/12304576) – nalzok May 10 '16 at 15:02
  • @sunqingyao Understood, I'll do better from now on out. Thanks! – Matthew Bradshaw May 10 '16 at 18:03
0

You can use a normal uitableview for this, for the "groups" you should divide your tableview in sections (in the code you have a delegate function which returns the number of sections)

For using a tableview in a UIViewController, you should implement from the UITableViewDelegate and UITableViewDataSource class. Then in your viewdidload you can place the name of your tableview.datasource = self and tableview.delegate = self, Then you can use the delegate functions from the tableview in your UIViewController.

after a quick google, I found an example which might be useful to you: LINK

Community
  • 1
  • 1
nickyhvm
  • 51
  • 4