簡體   English   中英

如何使用表格視圖單元格中的文本並將其顯示為父視圖控制器中的標簽?

[英]How can I use text from a table view cell and display it as a label in a parent view controller?

我試圖找出對我來說最好的方法,即使用UITableViewCell中的文本在其父視圖中設置/更改UILabel。 我希望當用戶連續點擊時完成此操作。 為了更加清楚,這是簡化的物理表示形式:

由此:

http://i.stack.imgur.com/O9ipE.png

為此,當用戶點擊該行時:

http://i.stack.imgur.com/fmeIb.png

我以為我可能只需要使用委托和協議,但是我將如何實際更改UILabel? 那不是將文本傳回,而不是傳回並更改UILabel嗎? 另外,我對放松的時間不太了解,但是我可以使用其中之一來代替它嗎?

是的,您將使用委托方法將數據傳遞回父級。 由於您正在使用某種數組數據源填充表,因此您已經可以以干凈的格式訪問這些值(而不是從tableViewCell的層次結構中提取數據)。

從表vc中:

@protocol MyTableViewDelegate
-(void)didSelectText:(NSString *)text;
@end

然后在您的父VC中實現-didSelectText:

當用戶選擇單元格時,您將擁有indexPath,它為您提供了一部分和一行。

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    // It's good practice to do this check first
    if([self.delegate respondsToSelector:@selector(didSelectText:)]){
          //basically pull the value out of your data source and send it to the delegate
        [self.delegate didSelectText: self.arrayDataSource[indexPath.section][indexPath.row]];
        [self dismissViewControllerAnimated:YES completion:nil];
    }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM