我有UITableView
和UISearchDisplayController
。UITableViewCell 是rowHeight = 30的自定义单元格:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *cellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
UILabel *lbTitle;
if (!cell) {
cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];
[cell setSelectionStyle:UITableViewCellSelectionStyleGray];
lbTitle = [[UILabel alloc]initWithFrame:CGRectMake(0, 0, tableView.frame.size.width, tableView.rowHeight)];
[lbTitle setTag:1];
[lbTitle setFont:[UIFont fontWithName:@"Sinhala Sangam MN" size:14]];
//[lbTitle setTextAlignment: NSTextAlignmentCenter];
[cell addSubview:lbTitle];
}
else {
lbTitle = (UILabel *)[cell viewWithTag:1];
}
}
如何在 UISearchDisplayController 中应用此单元格样式。因为当搜索处于“活动状态”时,UISearchDisplayController 的单元格看起来很基本。
谢谢
在MarkM的回答的帮助下解决(有没有办法自定义 UISearchDisplayController 单元格):
static NSString *normalCellReuseIdentifier = @"ANormalCell";
static NSString *searchCellReuseIdentifier = @"ASearchCell";
UITableViewCell* cell = nil;
UILabel *lbTitle;
if (tableView != self.searchDisplayController.searchResultsTableView) {
cell = [tableView dequeueReusableCellWithIdentifier:normalCellReuseIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:normalCellReuseIdentifier];
[cell setSelectionStyle:UITableViewCellSelectionStyleGray];
lbTitle = [[UILabel alloc]initWithFrame:CGRectMake(0, 0, tableView.frame.size.width, tableView.rowHeight)];
[lbTitle setTag:1];
[lbTitle setFont:[UIFont fontWithName:@"Sinhala Sangam MN" size:14]];
//[lbTitle setTextAlignment: NSTextAlignmentCenter];
[cell addSubview:lbTitle];
}
else lbTitle = (UILabel *)[cell viewWithTag:1];
} else {
tableView.rowHeight = 30;
cell = [tableView dequeueReusableCellWithIdentifier:searchCellReuseIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:searchCellReuseIdentifier];
[cell setSelectionStyle:UITableViewCellSelectionStyleGray];
lbTitle = [[UILabel alloc]initWithFrame:CGRectMake(0, 0, tableView.frame.size.width, tableView.rowHeight)];
[lbTitle setTag:1];
[lbTitle setFont:[UIFont fontWithName:@"Sinhala Sangam MN" size:14]];
//[lbTitle setTextAlignment: NSTextAlignmentCenter];
[cell addSubview:lbTitle];
}
else lbTitle = (UILabel *)[cell viewWithTag:1];
}