如何将数据放在UITableViewCell的右侧

时间:2016-05-16 15:50:50

标签: ios swift uitableview

我正在使用Xcode 7和Swift创建一个小任务列表应用程序。但是,我希望一些数据(如日期标签)位于UITableViewCell的右侧,除了左侧。我会用什么代码将日期标签放在UITableViewCell的右侧?

这是我的ViewController.swift的代码,它有TableView。

class ViewController: UIViewController, UITableViewDataSource, UITableViewDelegate {

// All Outlets Connected to StoryBoard
@IBOutlet var BTN: UIButton!
@IBOutlet var BTN2: UIButton!
@IBOutlet var BTN3: UIButton!
@IBOutlet var BTN4: UIButton!
@IBOutlet var tbl: UITableView?
@IBOutlet var Button: UIBarButtonItem!
@IBOutlet var Bar: UINavigationItem!

//Other Variables
var varView = Int()


  override func viewDidLoad() {
    super.viewDidLoad()
    // Do any additional setup after loading the view, typically from a nib.\





    if revealViewController() != nil {

        Button.target = revealViewController()
        Button.action = #selector(SWRevealViewController.revealToggle(_:))

    }


    self.view.backgroundColor = UIColor(red: 50 / 255.0, green: 132 / 255.0, blue: 255 / 255.0, alpha: 1.0)




    BTN.alpha = 1.0
    BTN4.alpha = 0


}

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


@IBAction func BTNClicked(sender: AnyObject) {

    UIView.animateWithDuration(1.0, animations: ({


        self.BTN2.transform = CGAffineTransformMakeTranslation(0, -90)
        self.BTN3.transform = CGAffineTransformMakeTranslation(0, -180)
        self.BTN4.transform = CGAffineTransformMakeTranslation(0, 0)
        self.BTN.alpha = 0
        self.BTN4.alpha = 1.0



    }))
}


@IBAction func BTNClickedAgain(sender: AnyObject) {

    UIView.animateWithDuration(1.0, animations: ({


        self.BTN2.transform = CGAffineTransformMakeTranslation(0, 0)
        self.BTN3.transform = CGAffineTransformMakeTranslation(0, 0)
        self.BTN4.transform = CGAffineTransformMakeTranslation(0, 0)
        self.BTN.alpha = 1.0
        self.BTN4.alpha = 0



    }))
}

func tableView(tableView: UITableView, commitEditingStyle editingStyle: UITableViewCellEditingStyle, forRowAtIndexPath indexPath: NSIndexPath){

    if(editingStyle == UITableViewCellEditingStyle.Delete){
        taskMgr.tasks.removeAtIndex(indexPath.row)
        tbl?.reloadData();
    }
}

override func viewWillAppear(animated: Bool) {
    tbl?.reloadData();
}



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

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

        let cell: UITableViewCell = UITableViewCell(style: UITableViewCellStyle.Subtitle, reuseIdentifier: "test")

        cell.textLabel?.text = taskMgr.tasks[indexPath.row].name
        cell.detailTextLabel?.text = taskMgr.tasks[indexPath.row].desc
        cell.detailTextLabel?.text = taskMgr.tasks[indexPath.row].date
        return cell
}

另外,这里是我的AddPlan.swift,这是我在ViewControlelr.swift中将数据添加到UITableViewCells的地方:

class addPlan: UIViewController, UITextFieldDelegate {


var time: Int = 6


@IBOutlet var txt: UITextField!

@IBOutlet var txt2: UITextField! // This is the data text, I want this to be on the left side of the UITableViewCell.
@IBOutlet var txt1: UITextField!
@IBOutlet var Button02: UIBarButtonItem!

override func viewDidLoad() {

    let tap: UITapGestureRecognizer = UITapGestureRecognizer(target: self, action: #selector(addPlan.dismissKeyboard))
    view.addGestureRecognizer(tap)

    self.txt.delegate = self;
    self.txt1.delegate = self;
    self.txt2.delegate = self;




    if revealViewController() != nil {

        Button02.target = revealViewController()
        Button02.action = #selector(SWRevealViewController.revealToggle(_:))

    }


}

func textFieldShouldReturn(textField: UITextField) -> Bool {
    self.view.endEditing(true)
    return false
}

func dismissKeyboard() {
    //Causes the view (or one of its embedded text fields) to resign the first responder status.
    view.endEditing(true)
}


@IBAction func ClickedforSelection(sender: UIButton) {
    taskMgr.addTask(txt.text!, desc: txt1.text!, date: txt2.text!)
    self.view.endEditing(true)
    txt.text = ""
    txt1.text = ""
    txt2.text = "" // This is the data text


}
}

1 个答案:

答案 0 :(得分:0)

您可以使用UITableViewCellStyle.Value1UITableViewCellStyle.Value2作为UITableViewCell样式。

来自Table View Styles and Accessory Views

  

UITableViewCellStyle.Value1将副标题放在蓝色文字中   右对齐它在行的右侧。图像不是   允许的。

     

虽然UITableViewCellStyle.Value2将主标题设为蓝色和   右对齐它从一个从左侧缩进的点对齐   行。字幕左对齐,距离右侧很近   这点。此样式不允许图像。