iphone - 网址检查无法正常工作

标签 iphone objective-c ios uitableview

在我的 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/

相关文章:

iphone - 如何使用 ARC 处理 iOS 中的自动释放对象

objective-c - 跳出 switch 语句

objective-c - 从 NSMutableDictionary 派生时 FastEnumeration 中明显的内存泄漏

ios - 异常 : addEvent is not a recognised Objective C method

iphone - 如何将多个自定义 View Controller 与导航 Controller 一起使用?

iphone - 有没有办法为 UITabBar 设置一个单独的短标题?

ios - 使用 Storyboard ios5 时从 View Controller 移动到选项卡栏 Controller

iphone - 如何在 Xcode 中的 _WebThreadLockFromAnyThread 上设置符号断点?

iOS : Not receiving facebook App invites

objective-c - 使用另一个项目的 Storyboard