UITableView DidSelectRowAtIndexPath?

前端 未结 5 792
余生分开走
余生分开走 2020-12-23 10:07

I have a table view with a few items (all text). When the user clicks the row I want that text in that cell to be added to an array.

How Do I grab the text out of th

5条回答
  •  旧时难觅i
    2020-12-23 10:53

    In general, you can find most answers to questions like this in the Apple documentation. For this case, look under UITableView Class Reference and you will see a section header: Accessing Cells and Sections. This give you the following method:

    – cellForRowAtIndexPath:
    

    Now use this to access the cell, and then use get the text from the cell (if you're unsure how, look under the UITableViewCell Class Reference.

    All together, your method might look something like this:

    -(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
        UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
        NSString *cellText = cell.textLabel.text;
        [myArray addObject:cellText];
    }
    

提交回复
热议问题