NSExplorer
NSExplorer

Reputation: 12149

Updating UITableViewCell dynamically

How can one change the textLabel or the detailTextLabel of a specific UITableViewCell dynamically?

Basically, I am downloading a set of files from the internet whenever the user taps on a cell. Now instead of the progress indicator, I would like to display the name of the file currently being downloaded. I would like to keep updating the detailTextLabel to show the (current) filenames until all the files are downloaded.

Upvotes: 0

Views: 1602

Answers (2)

Nathan Perry
Nathan Perry

Reputation: 300

Just in case anyone still has this problem I have had success with calls to the reloadData method. For example in my code in the viewWillAppear method, I have the line:

[tableSettings reloadData];

Where tableSettings is the name of my UITableView.

Upvotes: 0

Costa Walcott
Costa Walcott

Reputation: 887

Assuming you're in a method like tableView:didSelectRowAtIndexPath: (or have another way to get the index path to the cell), you can do something like this:

UITableViewCell *cell = [self.tableView cellForRowAtIndexPath:indexPath];

UILabel *label = nil;
for(UIView *subview in cell.contentView.subviews) {
    if([subview isKindOf:[UILabel class]]) {
        label = (UILabel*)subview;
        break;
    }
}

if(label)
    label.text = @"New text";

This will find the first UILabel in the cell, and may not work if your cell has more than one label. A more robust way to do it is to set the tag of the label when you create the cell (this can be done in Interface Builder or in code). Then you can find the label like this:

UITableViewCell *cell = [self.tableView cellForRowAtIndexPath:indexPath];

UILabel *label = nil;
for(UIView *subview in cell.contentView.subviews) {
    if([subview isKindOf:[UILabel class]] && [subview tag] == kDetailLabelTag) {
        label = (UILabel*)subview;
        break;
    }
}

if(label)
    label.text = @"New text";

Upvotes: 2

Related Questions