首页 > 解决方案 > 在 Tableview 中,如何在 Objective C 中获取所选行的 textview 值

问题描述

如何获取所选行的 textview 值,我在 google 中对此进行了搜索,但没有得到正确的解决方案。

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.

    arr = [[NSArray alloc]initWithObjects:@"Apple",@"ms-dynamicsxrm://?etn=thk_store&pagetype=entityrecord&id=7b74df21-e5a0-e711-810a-5065f38a9b71",@"Micromax",@"Nokia", nil];
} 

#pragma mark - Table View Data source
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:  (NSInteger)section {
    return arr.count;
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}

- (UITableViewCell *)tableView:(UITableView *)tableView  cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    static NSString *cellIdentifier = @"cell_id";

    SecondTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier: cellIdentifier];
    cell.label1.text = @"label";

    NSString *txtDetailStr = [arr objectAtIndex:indexPath.row];

    if ([txtDetailStr containsString:@"ms-dynamicsxrm://"]) {
        NSAttributedString *attributedString = [[NSAttributedString alloc] initWithString:txtDetailStr attributes:@{ NSLinkAttributeName: [NSURL URLWithString:txtDetailStr] }];
        cell.textview1.attributedText = attributedString;
    } 
    else {
        cell.textview1.text = txtDetailStr;
    }
    return cell;
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return 100;
}

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

}

如何获取所选行的 textview 值,我在 google 中对此进行了搜索,但没有得到正确的解决方案。

标签: iosobjective-ciphone

解决方案


您可以像这样获取所选行的 textview 值didSelectRowAtIndexPath

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    SecondTableViewCell *cell = [self.tableView cellForRowAtIndexPath:indexPath];
    NSLog(cell.textview1.text);

    // If you want to get selected row data from your array then you can access it like this.
    NSString *txtDetailStr = [arr objectAtIndex:indexPath.row];
    NSLog(txtDetailStr);
  }

推荐阅读