2014-07-22 71 views
0

基本上我有一个表格视图控制器,我正在使用事务填充。当用户点击添加按钮时,用户可以选择通过分段控件输入付款或信用额度。如何根据用户选择向表格视图单元格添加图像

我想知道如何根据用户选择的内容显示(例如)绿色箭头图像和每个单元格中的付款红色箭头?顺便说一句,我正在使用Swift。

这是细胞目前的样子。我希望箭头位于“金额”标签的左侧。

enter image description here

任何帮助将不胜感激。

这里是我目前拥有的代码...

ThirdViewController:

var arrayObject = ArrayData() 

class ThirdViewController: UITableViewController { 

@IBAction func addNewPaymentButton(sender : AnyObject) { 
    //Add code for adding a new payment here 
} 

override func viewWillAppear(animated: Bool) { 
    self.tableView.reloadData() 
} 

override func viewDidLoad() { 
    super.viewDidLoad() 

    // Uncomment the following line to preserve selection between presentations 
    // self.clearsSelectionOnViewWillAppear = false 

    // Uncomment the following line to display an Edit button in the navigation bar for this view controller. 
    // self.navigationItem.rightBarButtonItem = self.editButtonItem 
} 

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

// #pragma mark - Table view data source 

override func numberOfSectionsInTableView(tableView: UITableView?) -> Int { 
    // #warning Potentially incomplete method implementation. 
    // Return the number of sections. 
    return 1 
} 

override func tableView(tableView: UITableView?, numberOfRowsInSection section: Int) -> Int { 
    // #warning Incomplete method implementation. 
    // Return the number of rows in the section. 
    return arrayObject.paymentsArray().count 
} 


override func tableView(tableView: UITableView!, cellForRowAtIndexPath indexPath: NSIndexPath!) -> UITableViewCell! { 
    var cell:CustomTransactionTableViewCell = tableView.dequeueReusableCellWithIdentifier("Cell", forIndexPath: indexPath) as CustomTransactionTableViewCell 
    cell.paymentNameLabel.text = (arrayObject.paymentsArray().objectAtIndex(indexPath.row)) as String 
    cell.costLabel.text = (arrayObject.costArray().objectAtIndex(indexPath.row)) as String 
    cell.dateLabel.text = (arrayObject.dateArray().objectAtIndex(indexPath.row)) as String 

    if transactionType == 0 { 
     cell.paymentArrowImage.hidden = false 
    } else if transactionType == 1 { 
     cell.creditArrowImage.hidden = false 
    } 

    return cell 
} 

override func tableView(tableView: UITableView!, canEditRowAtIndexPath indexPath: NSIndexPath!) -> Bool { 
    return true 
} 

override func tableView(tableView: UITableView!, commitEditingStyle editingStyle: UITableViewCellEditingStyle, forRowAtIndexPath indexPath: NSIndexPath!) { 
    if (editingStyle == UITableViewCellEditingStyle.Delete) { 
     //Add code for deletion 
    } 
} 
} 

这是FifthViewController唯一的相关代码:

 arrayDataPayments.addObject(transactionName) 
    arrayDataCost.addObject("£\(finalUserBudget)") 
    arrayDataDate.addObject(transactionDateInputted) 

ArrayData

var arrayDataPayments: NSMutableArray = [] 
var arrayDataCost: NSMutableArray = [] 
var arrayDataDate: NSMutableArray = [] 

class ArrayData: NSObject { 

func paymentsArray() -> NSMutableArray { return arrayDataPayments } 

func costArray() -> NSMutableArray { return arrayDataCost } 

func dateArray() -> NSMutableArray { return arrayDataDate } 
} 

CustomTransactionTableViewCell

class CustomTransactionTableViewCell: UITableViewCell { 

    @IBOutlet var paymentNameLabel : UILabel 
    @IBOutlet var dateLabel : UILabel 
    @IBOutlet var costLabel : UILabel 
    @IBOutlet var creditArrowImage: UIImageView 
    @IBOutlet var paymentArrowImage: UIImageView 

    init(style: UITableViewCellStyle, reuseIdentifier: String) { 
     super.init(style: style, reuseIdentifier: reuseIdentifier) 
     // Initialization code 
    } 

    override func awakeFromNib() { 
     super.awakeFromNib() 
     // Initialization code 
    } 

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

     // Configure the view for the selected state 
    } 

} 

请原谅全局变量。他们只是暂时的。

+0

任何一段代码? – Shamsiddin

+0

我已将当前的代码添加为编辑。 – user3746428

回答

1

这将完全像其他表格视图一样工作。你的单元格中会有一个图像视图。无论图像是在该图像视图中显示的,还是显示的图像,都必须是您的模型的一部分,以便该模型负责知道关于每行的此信息。然后,在cellForRowAtIndexPath:中,您显示了来自视图中模型的信息,将图像视图的图像设置在单元格中,以查询您询问的任何行。

+0

我是Xcode的新手,所以我可能会做这个完全错误的,但我添加了两个图像到单元中的付款和信用,我把IBOutlets放在CustomTransactionTableViewCell类中。然后,我在第三个控制器的cellForRowAtIndexPath中放入一个if语句,根据用户的选择隐藏正确的图像。目前我正在收到一个未解决的标识符图像。我是否正确地做了这件事,如果是的话,我如何从另一个班级访问IBOutlets? – user3746428

+0

http://www.apeth.com/iOSBook/ch21.html#_custom_cells – matt

+0

感谢您的链接,但我通过在image.hidden之前添加标识符来解决问题。 – user3746428

相关问题