2016-08-19 110 views
1
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell { 

    let cell = self.tbl_vw.dequeueReusableCellWithIdentifier("cell", forIndexPath: indexPath)as!cuscellTableViewCell 

    cell.txt_lbl.text = self.section[indexPath.row]; 
    cell.sel_btn.addTarget(self, action: #selector(self.switchChanged), forControlEvents: .ValueChanged) 

    return cell 

} 



func switchChanged(sender: AnyObject) { 
    let switchControl: UISwitch = sender as! UISwitch 
    print("The switch is \(switchControl.on ? "ON" : "OFF")") 

    if switchControl.on { 
     print("The switch is on lets martch") 
    } 
} 

我在tableview中的自定義單元格中有一個開關和一個標籤。當我打開開關時,我需要獲得標籤中的文字,任何人都可以幫助如何使其成爲可能。如何從表格視圖中的自定義單元格中的標籤中獲取文本

+0

分享一些與自定義單元格相關的代碼,切換值更改操作。 – pkc456

+0

pkc456我添加了一些代碼。 – Arun

+0

我已經添加了解決方案代碼。 @阿倫請檢查。 – pkc456

回答

1

使用UISwitch的標籤屬性來存儲位置,並在您的處理程序中使用它來獲取實際的文本表單部分數組。

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

    let cell = self.tbl_vw.dequeueReusableCellWithIdentifier("cell", forIndexPath: indexPath)as!cuscellTableViewCell 

    cell.txt_lbl.text = self.section[indexPath.row]; 

    cell.sel_btn.tag = indexPath.row 

    cell.sel_btn.addTarget(self, action: #selector(self.switchChanged), forControlEvents: .ValueChanged) 

    return cell 

} 



func switchChanged(sender: AnyObject) 
{ 
    let switchControl: UISwitch = sender as! UISwitch 
    print("The switch is \(switchControl.on ? "ON" : "OFF")") 

    let text = self.section[switchControl.tag] 
    print(text) 

    if switchControl.on 
    { 
     print("The switch is on lets martch") 
    } 
} 

如果您有多個行的多個行,您可以使用字典並存儲元組。

var items = [Int:(Int,Int)]() 
... 

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

    let cell = self.tbl_vw.dequeueReusableCellWithIdentifier("cell", forIndexPath: indexPath)as!cuscellTableViewCell 

    cell.txt_lbl.text = self.section[indexPath.row]; 
    cell.sel_btn.tag = indexPath.row 

    let tuple = (section: indexPath.section, row: indexPath.row) 
    self.items[cell.sel_btn.hashValue] = tuple 
    cell.sel_btn.addTarget(self, action: #selector(self.switchChanged), forControlEvents: .ValueChanged) 

    return cell 

} 

func switchChanged(sender: AnyObject) 
    { 
     let switchControl: UISwitch = sender as! UISwitch 
     print("The switch is \(switchControl.on ? "ON" : "OFF")") 

     let tuple = self.items[switchControl.hashValue] 
     print(tuple.0) // this is the section 
     print(tuple.1) // this is the row 

     if switchControl.on 
     { 
      print("The switch is on lets martch") 
     } 
    } 
+0

如果UITableView中有多個部分,它將不起作用。 –

+1

令人敬畏的努力工作完美。 – Arun

+1

樂意提供幫助。 :) –

0

在你的情況下,將func switchChanged(sender: AnyObject)移動到你的自定義類並在customCell中爲UISwitch分配選擇器。您將開始在您的customCell中接收您的switchChange事件,現在您可以訪問您的對象,並且還可以保留一個布爾標誌以在需要時保持開關狀態。

  • 步驟1:將你的switchChanged到customCell類。
  • 步驟2:在customCell awakeFromNibinit方法分配選擇器UISwitch目的是細胞本身在細胞接開關改變事件。
  • 第3步:一旦您的切換更改事件觸發,您可以更新您的UILabel,因爲您可以在customCell內訪問它。

讓我知道你是否有任何疑問。

+0

Dipen坦克我會檢查它 – Arun

0

sel_btn(UISwitch)在cellForRowAtIndexPath方法的tag

sel_btn.tag = indexPath.row 

switchChanged方法,得到sel_btn的標籤,因此標籤的文本。

let switchControl: UISwitch = sender as! UISwitch 
     let tag = switchControl.tag 
     let cell : cuscellTableViewCell = tbl_vw.cellForRowAtIndexPath(NSIndexPath(forRow: tag, inSection: 0)) as? cuscellTableViewCell 
     print(cell.txt_lbl.text) 

如果你想從模型數據不是從視圖中,然後覆蓋在上面的兩行下面一行: -

let textValue = self.section[tag]; 
+0

如果UITableView中有多個部分,它將無法工作。 –

+0

如果有多個部分,請設置'tag = section +(10 * row)'。 – pkc456

+0

**不要**從視圖(單元格)中獲取文本從模型(數據源)獲取 – vadian

0

你需要做的是用塊

Get button click inside UI table view cell

第一移動你的操作方法對錶格單元格

中的tableview細胞加擋財產

@property (copy, nonatomic) void (^didTapButtonBlock)(id sender); 

與你的操作方法

調用塊

- (void)didTapButton:(id)sender { 
    if (self.didTapButtonBlock) 
    { 
     self.didTapButtonBlock(sender); 
    } 
} 

,並接收來自細胞,你有兩個部分和行有

 [cell setDidTapButtonBlock:^(id sender) 
    { 
     // Your code here 

    }]; 

希望它有幫助

0
In tableview delegate method - 

    tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) 

add index path value to switch tag value - 

cell.sel_btn.tag = indexPath.row 


In Switch Value change method just get selected text when that switch is ON 


func switchChanged(sender: AnyObject) 
{ 
    let switchControl: UISwitch = sender as! UISwitch 
    print("The switch is \(switchControl.on ? "ON" : "OFF")") 

    // If switch ON 
    if switchControl.on 
    { 
     print("The switch is ON」) 

     // Get Selected Label Text 
     let selectedText = self.section[switchControl.tag] 
     print(selectedText) 

    } 
} 
+0

如何在多個部分的表格視圖中實現此功能。 – Arun

+0

維護布爾數組,以顯示像的cellForRowAtIndexPath開關狀態: cell.mySwitch.tag = indexPath.row 如果(selectedFilterArray [indexPath.row] == 「否」){ cell.mySwitch.on =假 } 別的如果(selectedFilterArray [indexPath.row] == 「YES」){ cell.mySwitch.on =真 } –

+0

在數值變更方法: FUNC switchChange(是myswitch:UISwitch){ 打印(「標記值\(中間體(mySwitch.tag))「) if mySwitch。(「textName \(self.section [mySwitch.tag])」) } if!(selectedFilterArray [mySwitch.tag] =「打印」(「標記值\(Int(mySwitch.tag))」) = 「NO」){// 如果選中 mySwitch.on =真 } 其他{ //如果未選中 mySwitch.on =假 }} –

相關問題