-1

I have an app that retrieves the IPV4 and IPV6 addresses and as of now, the addresses are all run together in the UITableView. How do I list them on different lines? Below is my code.

import UIKit
import Foundation

class ViewController: UIViewController, UITableViewDelegate, UITableViewDataSource{

    @IBOutlet weak var tableView: UITableView!

    func getIFAddresses() -> [String] {
        var addresses = [String]()

        // Get list of all interfaces on the local machine:
        var ifaddr : UnsafeMutablePointer<ifaddrs> = nil
        if getifaddrs(&ifaddr) == 0 {

            // For each interface ...
            for (var ptr = ifaddr; ptr != nil; ptr = ptr.memory.ifa_next) {
                let flags = Int32(ptr.memory.ifa_flags)
                var addr = ptr.memory.ifa_addr.memory

                // Check for running IPv4, IPv6 interfaces. Skip the loopback interface.
                if (flags & (IFF_UP|IFF_RUNNING|IFF_LOOPBACK)) == (IFF_UP|IFF_RUNNING) {
                    if addr.sa_family == UInt8(AF_INET) || addr.sa_family == UInt8(AF_INET6) {

                        // Convert interface address to a human readable string:
                        var hostname = [CChar](count: Int(NI_MAXHOST), repeatedValue: 0)
                        if (getnameinfo(&addr, socklen_t(addr.sa_len), &hostname, socklen_t(hostname.count),
                            nil, socklen_t(0), NI_NUMERICHOST) == 0) {
                                if let address = String.fromCString(hostname) {
                                    addresses.append(address)

                                }
                        }
                    }
                }
            }
            freeifaddrs(ifaddr)
        }

        return addresses

    }


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

    func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return 1
    }

    func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
        var cell = UITableViewCell()
        cell.textLabel?.lineBreakMode = NSLineBreakMode.ByWordWrapping
        cell.textLabel?.numberOfLines = 0
        cell.textLabel?.text = self.getIFAddresses()[indexPath.row]
        cell.textLabel?.text = getIFAddresses().description
        return cell
    }
Ashish Kakkad
  • 23,586
  • 12
  • 103
  • 136

1 Answers1

0

With

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

func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    return 1
}

you have defined a table view which has one section with one single row, and

cell.textLabel?.text = getIFAddresses().description

puts the entire array (as a string) into the text label of the that single row.

What you should do is to call getIFAddresses() only once and save the result in a property:

var addresses : [String] = []

override func viewDidLoad() {
    super.viewDidLoad()
    addresses = getIFAddresses()
}

Then define a table view with one section and as many rows as you have addresses:

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

func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    return  addresses.count
}

Finally, fill each row with the corresponding address from the array:

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {

    let cell = UITableViewCell()
    // Or better (requires that you have a prototype cell registered):
    // let cell = self.tableView.dequeueReusableCellWithIdentifier("Cell", forIndexPath: indexPath) as! UITableViewCell
    cell.textLabel?.text = addresses[indexPath.row]
    return cell
}
Martin R
  • 529,903
  • 94
  • 1,240
  • 1,382