我正在使用自定义单元格类在swift中填充tableview。
我需要让它适用于一组按钮,以便每个单元格都有一个按钮,该按钮执行该单元格独有的IBAction功能。
这是我的单元格类,声明了按钮变量:
class taskCell: UITableViewCell {
@IBOutlet weak var performTask: UIButton!
//ignoring code irrelevant to question
}
按下按钮时我想要发生的所有“任务”都是以下模式的变体:
@IBAction func task1(sender: AnyObject){
if (prerequisite1 >= 1 ) {
prerequisite1 = prerequisite1 - 1;
points = points + 400;
prerequisite1Label.text = "\(prerequisite1)";
pointsLabel.text = "\(points)";
}
}
@IBAction func task2(sender: AnyObject){
if (prerequisite2 >= 1 ) {
prerequisite2 = prerequisite2 - 1;
points = points + 600;
prerequisite1Label.text = "\(prerequisite2)";
pointsLabel.text = "\(points)";
}
}
这是我尝试将它们放在我的viewController中的数组中,该表位于:
let firstTasks:[()->()] = [tasksViewController().task1(), tasksViewController().task2()]
最后,我尝试在tableview中实现它:
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCellWithIdentifier("cell", forIndexPath: indexPath) as! JobCell
cell.performTask.tag = indexPath.row
cell.performTask.addTarget(self, action: firstTasks[indexpath.row], forControlEvents: .TouchUpInside)
return cell
}
答案 0 :(得分:1)
而不是保持函数数组。你为什么不这样做:
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCellWithIdentifier("cell", forIndexPath: indexPath) as! JobCell
cell.actionBlock = {(sender) in
// do ur thing
}
return cell
}
在viewController中:
In .h file just Create
UIDatePicker *datePicker;
After that .m file
// For Date
datePicker = [[UIDatePicker alloc]init];
datePicker.datePickerMode = UIDatePickerModeDate;
[_closingDateTxtFld setInputView:datePicker];
UIToolbar *toolBar = [[UIToolbar alloc]initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 44)];
[toolBar setTintColor:[UIColor grayColor]];
UIBarButtonItem *doneBtn = [[UIBarButtonItem alloc]initWithTitle:@"Done" style:UIBarButtonItemStylePlain target:self action:@selector(ShowSelectedDate)];
UIBarButtonItem *space = [[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil];
[toolBar setItems:[NSArray arrayWithObjects:space,doneBtn, nil]];
[_closingDateTxtFld setInputAccessoryView:toolBar];