ios - 由于方法调用,Tableview 滚动速度变慢

标签 ios objective-c iphone uitableview

在我的表格 View 单元格中,有一个描述标签。描述文本将通过单独方法中的属性文本突出显示。此方法是从 cellforRowIndexPath 调用的,这就是 tableview 滚动滞后很多的原因。

我的代码

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
SearchVCCell *cell = (SearchVCCell *)[tableView dequeueReusableCellWithIdentifier:cellidentifier];
    if (cell == nil)
    {
        NSArray *nib = [[NSBundle mainBundle] loadNibNamed:@"SearchVCCell" owner:self options:nil];
        cell = [nib objectAtIndex:0];

    cell.lbl_title.text=[NSString stringWithFormat:@"%@",[arrTitle objectAtIndex:indexPath.row]];
    cell.lbl_disc.text=[NSString stringWithFormat:@"%@",[arrDescription objectAtIndex:indexPath.row]];
    cell.lbl_page.text=[NSString stringWithFormat:@"Page: %d",[[arrPageNumber objectAtIndex:indexPath.row]intValue]];


   int p= [self highlightText:_search_bar.text :cell.lbl_disc];   //Method Call
    cell.lbl_count.text=[NSString stringWithFormat:@"%d",p];
    }
    return cell;
}

高文本方法:

-(int) highlightText :(NSString *)srcTxt :(UILabel*)txtView {
    int srcTxtLen = srcTxt.length;
    int idx = 0,count=0;
    while (idx<(txtView.text.length-srcTxtLen)) {
        NSRange srcRange = NSMakeRange(idx, srcTxtLen);
        if ([[txtView.text substringWithRange:srcRange] isEqualToString:srcTxt]) {
            NSMutableAttributedString *tmpAttrTxt = [[NSMutableAttributedString alloc] initWithAttributedString:txtView.attributedText];
            [tmpAttrTxt addAttribute:NSBackgroundColorAttributeName value:[UIColor yellowColor] range:srcRange];
            txtView.attributedText = tmpAttrTxt;
            idx += srcTxtLen;
            count++;
        } else {
            idx++;
        }
    }
    return count;
}

帮我解决一下,提前致谢

最佳答案

你可以用这个替换你的方法调用

NSMutableAttributedString * string = [[NSMutableAttributedString alloc]initWithString:cell.lbl_disc.text];
NSRange range=[cell.lbl_disc.text rangeOfString:_search_bar.text];
[string addAttribute:NSForegroundColorAttributeName value:[UIColor yellowColor] range:range];
[self.textToSpeak setAttributedText:string];

关于ios - 由于方法调用,Tableview 滚动速度变慢,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25419736/

相关文章:

ios - 使用 NSPredicate 过滤 NSArray 的 Big-O 运行时

flash - 我的 Flash 调试器的地址是什么?

ios - NSInvalidArgumentException,使用 performSegueWithIdentifier 时发送到实例的无法识别的选择器

iphone - UIKeyboardTypeDecimalPad 中的小数点不能用于数学计算

objective-c - 如何处理仅从键盘在 ObjC 源代码中键入嵌套消息调用?

iphone - 异步调度递归 block

iphone - 如何检查是否设置了自定义委托(delegate)?

android - 移动分析中的事件持续时间?

ios - 不明白如何修复线程 1 : ECX_BAD_ACCESS (code = EXC_I386_GPFLT) (line chart swift iOS)

ios - 点击 UIButton 时如何避免触发 UIPanGestureRecognizer?