I have UITableView
and UISearchDisplayController
. UITableViewCell is a custom cell with 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];
}
}
How to apply this cell style in UISearchDisplayController. Because when search is "active" UISearchDisplayController's cell looks like basic.
Thanks
SOLVED by the help of MarkM's answer (Is there a way to customize UISearchDisplayController cell):
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];
}
All of the same UITableView delegate methods are called with the searchResultsTableView. Please reference the following answer for a more detailed explanation.
Is there a way to customize UISearchDisplayController cell
When
UISearchDisplayController
takes control of the table to display the results, it will callTo display the cell, so the only thing you have to do is detect when the displayController is calling this method:
Do the same with the rest of tghe methods of the delegate/datasource to implemente the search results cells as you want (for example, height or number of rows)
On your instance of
UISearchDisplayController
(assuming it's called myUISearchDisplayController) callmyUISearchDisplayController.searchResultsTableView.rowHeight = 30;
If row height is the only thing to change. setting it with
searchResultsTableView.rowHeight
works only for the first time. Tapping cancel and search with new search key ignore the custom rowHeight. (iOS 6.1, xcode 4.6.1) use the below.Search display controller has it's own table in which it shows the data which is searching.You can set the height by using this code-
Where SDC is instance of
UISearchDisplayController