我使用ExpandableView创建了一个菜单我有这个github存储库:
https://github.com/SubhiH/ExpandableTableView
实施后我需要知道如何知道单击单元格标签的时间,因为我需要在单击此标签时进行制作,然后将其重定向到另一个视图。 每个标签都会重定向到不同的视图,以便了解何时单击它们。 重定向我已经知道该怎么做但我试图检查标签何时被选中而不能。
有谁知道如何调整代码才能做到这一点?
下面是菜单图片和代码示例:
import UIKit
class ExpandableTableViewViewController: UIViewController,UITableViewDataSource,UITableViewDelegate {
@IBOutlet weak var tableView: UITableView!
var selectedCellIndexPath: NSIndexPath?
////Heigh for cell in subTableView
// Para pessoas que falam a língua portuguesa -> Tamanho da célula da tabela (principal e subs)
let selectedCellHeightForSubTable: CGFloat = 40.0
///Heigh for cell in Main Table in (unselected)
//Para pessoas que falam a língua portuguesa -> Tamanho da célula quando não escolhida
let unselectedCellHeight: CGFloat = 110.0
////Array to save references for SubTables
//Para pessoas que falam a língua portuguesa -> Neste array são salvas as referências para as subtabelas
var subTables:[UITableView] = [UITableView]();
///fake data /// tuples type
// Para pessoas que falam a língua portuguesa -> Aqui são instanciados os vetores que serão utilizados
// na instanciação das células principais que serão utilizadas no armazenamento das células que irão compor
// as tabelas e a subtabelas
var data:([String],[[String]],[[String]],[[String]])=([String](),[[String]](),[[String]](),[[String]()]);
override func viewDidLoad() {
super.viewDidLoad()
///Register Nib file for Main Cells
// Para pessoas que falam a língua portuguesa -> Aqui eu faço o registro dos arquivos nib que representam
// as células principais
tableView.registerNib(UINib(nibName: "ExpandableCellTableViewCell",bundle: nil), forCellReuseIdentifier: "cell");
////Initialize array with fake data
// Para pessoas que falam a língua portuguesa -> Aqui eu tenho a presença do método InitialiDataArray()
// que realiza a instanciação dos vetores das células principais e secundárias.
self.data = self.InitializeDataArray()
self.tableView.reloadData();
}
// Para pessoas que falam a língua portuguesa -> Retorna o número de seções na TableView
func numberOfSectionsInTableView(tableView: UITableView) -> Int {
return 1;
}
// Para pessoas que falam a língua portuguesa -> Ajusta as células da TableView nas subtabelas
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
///Main table
if tableView == self.tableView{
return data.0.count;
}else{
if let index = self.subTables.indexOf(tableView) {
return data.1[index].count+1;///+1 for Header cell in subtableView
}
return 0;
}
}
// Para pessoas que falam a língua portuguesa -> Ajuste das células presentes nas linhas (subtabelas)
func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat {
if tableView == self.tableView {
if selectedCellIndexPath == indexPath {
////unselected Heigh + heigh for every cell in subtable
return unselectedCellHeight+selectedCellHeightForSubTable*CGFloat(self.data.1[indexPath.row].count+1)
}
return unselectedCellHeight
}else{
return selectedCellHeightForSubTable
}
}
// Para pessoas que falam a língua portuguesa -> Processo de seleção das células e inclusão dos efeitos
// de expandir e colapsar.
func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
if tableView == self.tableView {
if selectedCellIndexPath != nil && selectedCellIndexPath == indexPath {
let cell = self.tableView.cellForRowAtIndexPath(indexPath) as! ExpandableCellTableViewCell;
//let cell2 = self.tableView.cellForRowAtIndexPath(indexPath)!
//let textoo = (cell2.viewWithTag(4) as! UILabel).text
// print("Yuri 2")
// print(textoo)
cell.directionImageView.image = UIImage(named: "down");
tableView.cellForRowAtIndexPath(indexPath)?.viewWithTag(3)!.hidden = true;
selectedCellIndexPath = nil
} else {
selectedCellIndexPath = indexPath
let cell = self.tableView.cellForRowAtIndexPath(indexPath) as! ExpandableCellTableViewCell;
cell.directionImageView.image = UIImage(named: "up");
tableView.cellForRowAtIndexPath(indexPath)?.viewWithTag(3)!.hidden = false;
}
tableView.beginUpdates()
tableView.endUpdates()
if selectedCellIndexPath != nil {
// This ensures, that the cell is fully visible once expanded
tableView.scrollToRowAtIndexPath(indexPath, atScrollPosition: .None, animated: true)
}
}
}
// Para pessoas que falam a língua portuguesa -> Efeitos de expansão e colapso quando a célula não foi
//selecionada
func tableView(tableView: UITableView, didDeselectRowAtIndexPath indexPath: NSIndexPath) {
if self.tableView.isEqual(tableView){
let cell = self.tableView.cellForRowAtIndexPath(indexPath) as! ExpandableCellTableViewCell;
cell.directionImageView.image = UIImage(named: "down");
cell.viewWithTag(3)!.hidden = true;
}
}
// Para pessoas que falam a língua portuguesa -> Preenchimento do conteúdo dos vetores nas células principais
// e secundárias
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
var cell:UITableViewCell=UITableViewCell();
if tableView == self.tableView{
cell = tableView.dequeueReusableCellWithIdentifier("cell", forIndexPath: indexPath)
as! ExpandableCellTableViewCell;
(cell.viewWithTag(1) as! UILabel).text = self.data.0[indexPath.row]
let subtable = cell.viewWithTag(3) as! UITableView;
subtable.delegate = self;
subtable.dataSource = self;
subtable.registerNib(UINib(nibName: "SubTableCellTableViewCell",bundle: nil), forCellReuseIdentifier: "sub_cell");
self.subTables.append(subtable);
subtable.reloadData();
}else{
cell = tableView.dequeueReusableCellWithIdentifier("sub_cell", forIndexPath: indexPath)
print(indexPath.row)
print(indexPath.section)
if indexPath.row>0{
if let index = self.subTables.indexOf(tableView) {
(cell.viewWithTag(1) as! UILabel).text = self.data.1[index][indexPath.row-1]
(cell.viewWithTag(2) as! UILabel).text = self.data.2[index][indexPath.row-1]
(cell.viewWithTag(3) as! UILabel).text = self.data.3[index][indexPath.row-1]
// let texto = (cell.viewWithTag(1) as! UILabel).text
// print(texto)
}
}
}
// if("verdade".isEqualToString("verdade"))
//{
// print("Entrei")
//}
cell.selectionStyle = .None
// let tex = (cell.viewWithTag(1) as! UILabel).text
// print("Yuri")
// print(tex)
return cell;
}
// Para pessoas que falam a língua portuguesa -> Declaração do conteúdo das células principais e
// secundárias
public func InitializeDataArray()->([String],[[String]],[[String]],[[String]]){
var mainCells = [String]();
var col1_SubCells = [[String]]();
var col2_SubCells = [[String]]();
var col3_SubCells = [[String]]();
////Expandable Cells Data
mainCells.append("Genesis");
mainCells.append("Exodus");
mainCells.append("Leviticus");
mainCells.append("Numbers");
// mainCells.append("Cell 5");
////First Main cell data
///First Col////////
var tempArr:[String] = [String]();
tempArr.append("Chapters")
tempArr.append("Gen 3")
tempArr.append("Gen 6")
tempArr.append("Gen 9")
col1_SubCells.append(tempArr);
///2nd Col
tempArr = [String]();
tempArr.append("")
tempArr.append("Gen 2")
tempArr.append("Gen 5")
tempArr.append("Gen 8")
col2_SubCells.append(tempArr);
///3rd Col
tempArr = [String]();
tempArr.append("")
tempArr.append("Gen 1")
tempArr.append("Gen 4")
tempArr.append("Gen 7")
col3_SubCells.append(tempArr);
////2nd Main cell data////////
///First Col
tempArr = [String]();
tempArr.append("Chapters")
tempArr.append("Exo 3")
tempArr.append("Exo 6")
tempArr.append("Exo 9")
col1_SubCells.append(tempArr);
///2nd Col
tempArr = [String]();
tempArr.append("")
tempArr.append("Exo 2")
tempArr.append("Exo 5")
tempArr.append("Exo 8")
col2_SubCells.append(tempArr);
///3rd Col
tempArr = [String]();
tempArr.append("")
tempArr.append("Exo 1")
tempArr.append("Exo 4")
tempArr.append("Exo 7")
col3_SubCells.append(tempArr);
////3rd Main cell data////////
///First Col
tempArr = [String]();
tempArr.append("Chapters")
tempArr.append("Lev 3")
tempArr.append("Lev 6")
tempArr.append("Lev 9")
col1_SubCells.append(tempArr);
///2nd Col
tempArr = [String]();
tempArr.append("")
tempArr.append("Lev 2")
tempArr.append("Lev 5")
tempArr.append("Lev 8")
col2_SubCells.append(tempArr);
///3rd Col
tempArr = [String]();
tempArr.append("")
tempArr.append("Lev 1")
tempArr.append("Lev 4")
tempArr.append("Lev 7")
col3_SubCells.append(tempArr);
////4th Main cell data////////
///First Col
tempArr = [String]();
tempArr.append("Chapters")
tempArr.append("Num 3")
tempArr.append("Num 6")
tempArr.append("Num 9")
col1_SubCells.append(tempArr);
///2nd Col
tempArr = [String]();
tempArr.append("")
tempArr.append("Num 2")
tempArr.append("Num 5")
tempArr.append("Num 8")
tempArr.append("Num 11")
col2_SubCells.append(tempArr);
///3rd Col
tempArr = [String]();
tempArr.append("")
tempArr.append("Num 1")
tempArr.append("Num 4")
tempArr.append("Num 7")
tempArr.append("Num 10")
col3_SubCells.append(tempArr);
return (mainCells,col1_SubCells,col2_SubCells,col3_SubCells);
}
}
当我点击col3_row1等中的示例时,看到我想知道的图像,而不是在主单元格中。
答案 0 :(得分:0)
如果你搜索这样的解决方案:
let label = tableView.cellForRowAtIndexPath(indexPath)?.viewWithTag(3)!
if (CGRectContainsPoint(label.frame, pointInCell)) {
// user tapped image
} else {
// user tapped cell
}
然后这个解决方案可以完美地为您量身定做:
答案 1 :(得分:0)
使用标签的手势属性并在其中指定点击手势,您可以通过该手势触发标签触摸上的方法。这是代码:
//Creating Label
actionlabel = UILabel()
//Setting Frame
actionlabel.frame = CGRectMake(...) // Provide label frame accordingly
//Adding TapGesture to make UILabel touchable
let tapGesture = UITapGestureRecognizer(target: self, action: "fireActionMethod:")
谢谢。