Supertecnoboff
Supertecnoboff

Reputation: 6616

How to get UITableView Label Text string - Custom Cell

I have a UITableView with CustomCell. The custom cells contain a UILabel and a UIImageView.

I saw online that it is possible to get the text from a normal UITableView cell and store it in a string when the user presses the cell.

But how can you do this when you are using a custom cell? Becuase the name of my UILabel is "type_label" and it has been defined in the header file of my CustomCell XIB.

So in Xcode I can't use:

cell.type_label.text"

In the following function:

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

Thanks, Dan.

Upvotes: 4

Views: 9185

Answers (2)

bhavya kothari
bhavya kothari

Reputation: 7474

Try below code - You have create type_label label's IBOutlet in CCustomCellClass
(file-->new-->subclass--> UITableViewCell-->name it like CustomCellClass)
Then implement below code

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

static NSString *cellIdentifier = @"Cell";

static BOOL nibsRegistered = NO;

if (!nibsRegistered) {

    UINib *nib = [UINib nibWithNibName:@"CustomCellClass" bundle:nil];
    [tableView registerNib:nib forCellReuseIdentifier:cellIdentifier];
}

CustomCellClass *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];

if (cell == nil) {

    cell = [[CustomCellClass alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];
}

cell.type_label.text = @"Rocky"; // setting custom cell label

return  cell;

}

Upvotes: -1

CW0007007
CW0007007

Reputation: 5681

In the tableViewDidSelectRow all you need to do is:

UITableViewCellCustomClass *customCell = (UITableViewVellCustomClass*)[tableView cellForRowAtIndexPath:indexPath];

NSString *labelText = [[customCell type_label] text];

That should get you want you need.

Upvotes: 12

Related Questions