使用swift

时间:2017-04-15 17:58:53

标签: ios swift

各位大家好,我很想知道你是否可以更新按钮内的单元格?我已经按下了按钮的代码,它得到你的lat / lng并打印出来,但现在我正在尝试更新我的其他swift文件中的Lat和Lng单元格 CoordinatesAltitudeDegreesTableViewCell.swift 截至目前我的tableview函数似乎工作,我测试更新viewController函数中的单元格,它的工作原理。

我的问题是将这些函数放在按钮中,以便lat / lng变量在范围内

旁注:我一直在寻找解决方案两天,我似乎找到任何帮助我的页面,希望这不是一个转发,如果有事先抱歉

*再次,对不起,我对swift编程很陌生

import UIKit
import Alamofire
import SwiftyJSON
import CoreLocation
import MapKit

class myTableViewController: UIViewController, UITableViewDelegate, UITableViewDataSource, CLLocationManagerDelegate{

//----------------------
// Buttons and Variables
//----------------------
var lastLocation: CLLocation? = nil
@IBOutlet weak var tableView: UITableView!
@IBAction func generateInfo(sender: AnyObject) {

    locationManager.requestWhenInUseAuthorization()
    locationManager.startUpdatingLocation()
    let lat: Double! = lastLocation?.coordinate.latitude
    let lng: Double! = lastLocation?.coordinate.longitude
    let alt: Double! = lastLocation?.altitude
    print(lat)
    print(lng)
    print(alt)
    locationManager.stopUpdatingLocation()

} // end of button

//--------------
// Main Function
//--------------
override func viewDidLoad(){
    super.viewDidLoad()

    print("Hello World")

    self.tableView.dataSource = self
    self.tableView.delegate = self
}

override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
    // Dispose of any resources that can be recreated.
}


//-------------------
// LOCATION FUNCTIONS
//-------------------
// Represents Location Manager
lazy var locationManager: CLLocationManager = {
    let manager = CLLocationManager()
    manager.delegate = self
    manager.desiredAccuracy = kCLLocationAccuracyNearestTenMeters
    return manager
}()
// Location Authorization Function
func locationManager(_ manager: CLLocationManager, didChangeAuthorization status: CLAuthorizationStatus){
    if case .authorizedWhenInUse = status {
        manager.requestLocation()
    } else {
        print("yeah... that didn't work")
    }
}
// Location Error handle
func locationManager(_ manager: CLLocationManager, didFailWithError error: Error) {
    print("that didn't work")
}

// Location Object
func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]){

    if let location = locations.first {
        lastLocation = location
    }
}

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

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

    return 10
}


public func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {

    if indexPath.row == 0 {
        let cell = tableView.dequeueReusableCell(withIdentifier: "imageCellCustomCell", for: indexPath) as! ImageCellTableViewCell


        return cell
    }
    else if indexPath.row == 1{
        let cell = self.tableView.dequeueReusableCell(withIdentifier: "coordinatesAltitudeDegreesCustomCell", for: indexPath) as! CoordinatesAltitudeDegreesTableViewCell
        cell.latLabel.text = "text"
        return cell
    }
    else if indexPath.row == 2{
        let cell = tableView.dequeueReusableCell(withIdentifier: "censusGeographyCustomCell", for: indexPath) as! CensusGeographyTableViewCell
        return cell
    }
    else if indexPath.row == 3{
        let cell = tableView.dequeueReusableCell(withIdentifier: "nearestAddressCustomCell", for: indexPath) as! NearestAddressTableViewCell
        return cell
    }
    else if indexPath.row == 4{
        let cell = tableView.dequeueReusableCell(withIdentifier: "populationCustomCell", for: indexPath) as! PopulationTableViewCell
        return cell
    }
    else if indexPath.row == 5{
        let cell = tableView.dequeueReusableCell(withIdentifier: "ethnicityCustomCell", for: indexPath) as! EthnicityTableViewCell
        return cell
    }
    else if indexPath.row == 6{
        let cell = tableView.dequeueReusableCell(withIdentifier: "raceCustomCell", for: indexPath) as! RaceTableViewCell
        return cell
    }
    else if indexPath.row == 7{
        let cell = tableView.dequeueReusableCell(withIdentifier: "ageCustomCell", for: indexPath) as! AgeTableViewCell
        return cell
    }
    else if indexPath.row == 8{
        let cell = tableView.dequeueReusableCell(withIdentifier: "populationPyramidCustomCell", for: indexPath) as! PopulationPyramidTableViewCell
        return cell
    }
    else {
        let cell = tableView.dequeueReusableCell(withIdentifier: "houseHoldCustomCell", for: indexPath) as! HouseHoldTableViewCell
        return cell
    }
}

func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
    if indexPath.row == 0 {
        return 300
    }
    else if indexPath.row == 1{
        return 130
    }
    else if indexPath.row == 2{
        return 100
    }
    else if indexPath.row == 3{
        return 100
    }
    else if indexPath.row == 4{
        return 90
    }
    else if indexPath.row == 5{
        return 110
    }
    else if indexPath.row == 6{
        return 200
    }
    else if indexPath.row == 7{
        return 300
    }
    else if indexPath.row == 8{
        return 300
    }
    else {
        return 360
    }
}

}

1 个答案:

答案 0 :(得分:0)

您的班级中应该有lat, long, alt的变量,而CoordinatesAltitudeDegreesTableViewCell中的变量也应该包含这些变量。

例如,这是一段代码片段。

class SomeViewController: UIViewController {

    // Set these attributes here cause they will be used as your source of data for the UITableViewCell
    var lat: Int = 0  
    var lon: Int = 0
    var alt: Int = 0

    @IBOutlet var tableView: UITableView!
    @IBOutlet var someButton: UIButton!

    override func viewDidLoad() {
        super.viewDidLoad()

        someButton.addTarget(self, action: #selector(self.someButtonClicked(_:)), forControlEvents: .TouchUpInside)
    }

    func someButtonClicked(sender: UIButton) {
        lat = 100 // set to different values
        lon = 10 // set to different values
        alt = 30 // set to different values

        // when you reload the tableView all the `UITableViewDelegate, UITableViewDataSource` codes will be called again therefore refreshing your cells.
        tableView.reloadData()
    }
}

extension SomeViewController: UITableViewDelegate, UITableViewDataSource {
    func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: "SomeCustomCell", for: indexPath) as! SomeCustomCell

        // set the parameters
        cell.lat = self.lat 
        cell.lon = self.lon
        cell.alt = self.atl

        return cell
    }

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

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

class SomeCustomCell: UITableViewCell {
    var lat: Int = 0 {
        didSet { // just in case you don't know about didSet there's an explanation below
            lblLat.text = "\(lat)"
        }
    }
    var lon: Int = 0 {
        didSet {
            lblLon.text = "\(lon)"
        }
    }
    var alt: Int = 0 {
        didSet {
            lblAlt.text = "\(alt)"
        }
    }

    ... insert some ui objects here 
    @IBOutlet var lblLat: UILabel!
    @IBOutlet var lblLon: UILabel!
    @IBOutlet var lblAlt: UILabel!
    ... insert some ui objects here

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

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

有很多方法可以做到这一点,但这是我能想到的最简单的方法。

didSet说明

var someVariable: <SomeDataType> = <SomeValue> {
    didSet {
        // this block is called whenever this variable's value is changed (i.e someVariable)
    }
}

例如:

var name: String = "" {
    didSet {
        print(name)
    }
}

init() {
    name = "Capaldi"
    name = "Tennant"
    name = "Smith"
    name = "Eccleston"
}

在你的调试器中,它会输出类似这样的内容,因为每次设置名称

时都会调用didSet
Capaldi
Tennant
Smith
Eccleston