Anil Pahadiya
Anil Pahadiya

Reputation: 11

Get data from UITableViewCell

I want to get data from UITableViewCell. here is my code.

[selection CreateTableview:colorList withSender:sender  withTitle:@"Please Select" setCompletionBlock:^(int tag){

        NSLog(@"Tag--->%d",tag);
        NSLog(@"Selected Row %ld",(long)indexPath.row);
        updated_lenses = tag;

        [self.tableview reloadData];

    }];

I got selected index here but I want to get value inside that cell how can I get it.? NOTE: I am not using DidSelectRowAtIndexPath Method.

Upvotes: 0

Views: 261

Answers (2)

AechoLiu
AechoLiu

Reputation: 18368

As for me, there are 2 approaches to get data from cell.

Method 1.
Put necessary data inside the subclass of UITableViewCell.

class MyTableViewCell: UITableViewCell {
    var property1: String = ""
    var property2: Int = 2
}

Method 2.
Put data in a struct or class, and let it be data source for table view.

struct CellData {
    var property1: String = ""
    var property2: Int = 0
}
var cellDataList: [CellData] = [] ///< This decide how many cell for table view.

// For UITableView DataSource & Delegate
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    return cellDataList.count
}

func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
    let data = cellDataList[indexPath.row]
    // .....
}

Upvotes: 1

ketaki Damale
ketaki Damale

Reputation: 664

NSIndexPath *index = [NSIndexPath indexPathForRow:tag inSection:0];
UITableViewCell *cell = [self.tableView cellForRowAtIndexPath:index];

Upvotes: 0

Related Questions