In my TableViewController I’m using custom cells. I’m displaying some data about the companies. Company title is colored blue by default, and I’m checking if company does not have valid url so I can paint it black. Of course, I added a logic to forbid opening companies website if the company does not have valid Url.
However, every time this table view is displayed, there are several companies with valid urls who’s titles are painted black.
So, I’m using same logic for coloring the titles and opening urls in safari, but coloring does not work properly, and opening in safari does.
Any ideas what’s wrong here?
Here’s my cellForRowAtIndexPath function
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"StandardSearchResultCell";
ResultsViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
// Configure the cell...
Data *theData = [Data getInstance];
Company *theCompany = [theData.results objectAtIndex:indexPath.row];
cell.lblTitle.text = theCompany.DisplayName;
cell.lblDescription.text = theCompany.Description;
cell.lblAddressPt1.text = theCompany.AddressPt1;
cell.lblAddressPt2.text = theCompany.AddressPt2;
cell.lblPhone.text = theCompany.Phone;
cell.lblEmail.text = theCompany.Email;
cell.lblDescription.adjustsFontSizeToFitWidth = false;
cell.lblDescription.lineBreakMode = UILineBreakModeWordWrap;
cell.lblDescription.numberOfLines = 0;
[cell.lblDescription sizeToFit];
NSURL *candidateURL = [NSURL URLWithString:theCompany.Url];
NSLog(@"%@", candidateURL);
if (!(candidateURL && candidateURL.scheme && candidateURL.host)) {
cell.lblTitle.textColor = [UIColor colorWithRed:0 green:0 blue:0 alpha:1];
}
return cell;
}
and this is the didSelectRowAtIndexPath
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
Company *company = [[Data getInstance].results objectAtIndex:indexPath.row];
NSURL *candidateURL = [NSURL URLWithString:company.Url];
if (candidateURL && candidateURL.scheme && candidateURL.host) {
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:company.Url]];
}
}
You need to set cell.lblTitle.textColor explicitly always, as you might dequeue a cell with black label. So add an else clause and make sure you’re setting cell.lblTitle.textColor to some value always: