2013-01-16 12 views
2

我是iOS應用程序開發新手,我需要關於UItableview的幫助。因爲我用JSON數據填充table-view。我也使用Customized Tableviewcell。 我沒有得到「lbluid」上選定單元格的「huid」值。 「lbluid」根據滾動顯示「huid」的值,因爲我將向上滾動它將顯示UITableview的最上面可見單元格的「huid」值,如果向下滾動它將顯示last的「huid」值可見細胞從下往上。如何從選定的UITableview行獲取cell.label.text值?

- (UITableViewCell *)tableView:(UITableView *)tableView 
    cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    static NSString *simpleTableIdentifier = @"SimpleTableCell"; 
    SimpleTableCell *cell = (SimpleTableCell *)[tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier]; 
    if (cell == nil) { 
     NSArray *nib = [[NSBundle mainBundle] loadNibNamed:@"SimpleTableCell" owner:self options:nil]; 
     cell = [nib objectAtIndex:0];; 
    } 

    NSUInteger row = [indexPath row]; 

    cell.lbl4.text = [hudid objectAtIndex:row]; 
    lbluid.text=cell.lbl4.text; 
    return cell; 
} 

Simpletablecell是定製UITableViewCell

回答

11

您可能需要實施方法:

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath 

它一旦被選中,每次調用,這個方法裏面:

Simpletablecell *cell = (Simpletablecell *)[tableView cellForRowAtIndexPath:indexpath]; 
lbluid.text = cell.lbl4.text; 

的方法:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 

被稱爲表格視圖正在加載它的單元格來顯示,這就是爲什麼你lbluid.text我只有在滾動表格視圖時纔會改變。

+0

非常感謝你的幫助@Andrew Tetlaw它的工作對我來說,即使我得到你的答案之前,這個解決方案,但其提出警告「兼容的指針類型初始化‘Simpletablecell *’...... ....「 – Atul

+0

確保類名稱正確:'Simpletablecell'並且它也是'UITableViewCell'的子類。還有什麼是該錯誤信息的其餘部分?它應該有一些額外的有用信息。 –

+0

這種類型的單元格是否存在任何風險? – AnthonyM

0

在斯威夫特

func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) { 

     let cell = tableView.cellForRow(at: indexPath) as! NotesCell! 
     let value = cell?.lblDetail.text 
    } 
相關問題