我在同一个视图控制器中有2个tableviews:“studentsTable”和“professorsTable”。我想为两个表创建一个标准的头部单元。我将UItableViewCell子类化并创建了headCell类型。然后在indexPath.row == 0的情况下为其分配一个headCell。一切顺利,然而,有一个问题。每当我点击“addingButton”时,我想要“addStudent”函数被调用,如果它在“studentsTable”和“addProfessor”上,如果它在“ProfessorsTable”上。显然我可以创建两个headCells,每个表一个,但我想知道是否有更专业的方法来处理这个,编写更少的代码。 如果给出了正确的“if”条件,我认为这些代码可能会起作用。 提前致谢
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell{
var actionType:String=String()
if(/*WHAT CONDITION SHOULD I PUT?*/)
{actionType="addStudent"}
else
{actionType="addProfessor"}
if(indexPath.row==0)
{
var theHeadcell : headCell = tableView.dequeueReusableCellWithIdentifier("headCell") as headCell
// cell setup
if(actionType != "")
{
addButton.addTarget(self, action: NSSelectorFromString(actionType), forControlEvents: UIControlEvents.TouchUpInside)
}
return theHeadCell
}
else
{
if (tableView==self.studentsTable)
{
var cell : UITableViewCell = tableView.dequeueReusableCellWithIdentifier("studentsCell") as UITableViewCell
// cell setup
return cell
}
else
{
var cell : UITableViewCell = tableView.dequeueReusableCellWithIdentifier("ProfessorsCell") as UITableViewCell
// cell setup
return Cell
}
}
}
答案 0 :(得分:1)
if tableView == self.studentsTable
{actionType = "addStudent"}
else if tableView == self.professorsTable
{actionType = "addProfessor"}