I have a table view with a custom protoype cell and the cell has 3 different labels on it. How do I get access to these labels? I need to change their texts. I've searched around everywhere and haven't found something that helps me in this case. I have the below code:
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"basicCell" forIndexPath:indexPath];
//change cell's 3 labels' text here
return cell;
}
How can I access the above cell's 3 different labels that it has?
You need to make a class that is a subclass of UITableViewCell
, and set the custom cell's class to the class you made. Then you want to link the labels using IBOutlet
s in the cell to the subclass you made.
Finally, in -tableView:cellForRowAtIndexPath:
, you should cast the cell to your new subclass so you get access to those IBOutlet
s. Assuming your outlets are named someLabelOutlet1
, someLabelOutlet2
, and someLabelOutlet3
, do the following after you finished subclassing.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
SomeTableViewCellSubclass *cell = (SomeTableViewCellSubclass *)[tableView dequeueReusableCellWithIdentifier:@"basicCell" forIndexPath:indexPath];
cell.someLabelOutlet1.text = @"sometext"
cell.someLabelOutlet2.text = @"sometext"
cell.someLabelOutlet3.text = @"sometext"
return cell;
}
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments