在我的 TableViewController 中,我使用自定义单元格。我正在显示有关公司的一些数据。公司名称默认为蓝色,我正在检查公司是否没有有效的 url,以便将其涂成黑色。当然,如果公司没有有效的Url,我添加了禁止打开公司网站的逻辑。
但是,每次显示此 TableView 时,都有几家具有有效 url 的公司的标题被涂黑。
因此,我使用相同的逻辑为标题着色并在 safari 中打开 url,但着色无法正常工作,而在 safari 中打开则可以。
知道这里出了什么问题吗?
这是我的 cellForRowAtIndexPath 函数
- (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;
}
这是 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]];
}
}
最佳答案
您需要始终明确设置 cell.lblTitle.textColor,因为您可能会出列带有黑色标签的单元格。因此,添加一个 else 子句并确保始终将 cell.lblTitle.textColor 设置为某个值:
if (!(candidateURL && candidateURL.scheme && candidateURL.host)) {
cell.lblTitle.textColor = [UIColor colorWithRed:0 green:0 blue:0 alpha:1];
else
cell.lblTitle.textColor = [UIColor colorWithRed:0 green:0 blue:1 alpha:1];
关于iphone - 网址检查无法正常工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12377102/