2015-07-10 48 views
-1

我有一个应用程序,它可以检索IPV4和IPV6地址,并且截至目前,地址都在UITableView中一起运行。我如何在不同的行上列出它们?以下是我的代码。在不同的行上列出单独的IP地址

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 
    } 
+0

可以添加u're tableView的屏幕截图吗? –

+1

该代码看起来很熟悉http://stackoverflow.com/a/25627545/1187415 :) –

回答

0

随着

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

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

您所定义,其具有与一个单列, 和

一个区段
cell.textLabel?.text = getIFAddresses().description 

使整个阵列(字符串)到一个表视图该单行的文本标签。

你应该做的是调用getIFAddresses()一次和 结果保存在一个属性:

var addresses : [String] = [] 

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

然后用一个部分,并尽可能多的行定义一个表视图,你 有地址:

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

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

最后,填充每个行与从所述阵列的相应地址:

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 
} 
+0

感谢您的信息。这样可行! –