如下,實現(xiàn)相關(guān)代理方法,
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"pool"];
// 顯示搜索結(jié)果時
if (self.searchController.active) {
// 原始搜索結(jié)果字符串.
NSString *originResult = self.arrOfSeachResults[indexPath.row];
// 獲取關(guān)鍵字的位置
NSRange range = [originResult rangeOfString:self.searchController.searchBar.text];
// 轉(zhuǎn)換成可以操作的字符串類型.
NSMutableAttributedString *attribute = [[NSMutableAttributedString alloc] initWithString:originResult];
// 添加屬性(粗體)
[attribute addAttribute:NSFontAttributeName value:[UIFont systemFontOfSize:20] range:range];
// 關(guān)鍵字高亮
[attribute addAttribute:NSForegroundColorAttributeName value:[UIColor redColor] range:range];
// 將帶屬性的字符串添加到cell.textLabel上.
[cell.textLabel setAttributedText:attribute];
cell.textLabel.text = self.arrOfSeachResults[indexPath.row];
} else {
cell.textLabel.text = self.arrOfSeachBoxes[indexPath.row];
}
return cell;
}