簡體   English   中英

有沒有一種方法可以自定義UISearchDisplayController單元格

[英]Is there a way to customize UISearchDisplayController cell

我正在使用UISearchDisplayController進行搜索,如果可能的話,我想向每個單元格添加更多元素。 進行了一些搜索,但找不到任何信息。 唯一的方法是使用tableview復制它。 除了textlabel.text之外,我還可以設置其他任何屬性嗎?

提前致謝!

您可以完全更改單元格。

以下代碼段依賴於UITableCell的兩個子類ANormalCell和ASearchCell。

警告:該代碼尚未編譯,但是您能理解嗎?

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

    static NSString *normalCellReuseIdentifier = @"ANormalCell";
    static NSString *searchCellReuseIdentifier = @"ASearchCell";

    UITableViewCell* cell = nil;

    if (tableView != self.searchDisplayController.searchResultsTableView) {
        cell = (ANormalCell*)[self.tableView dequeueReusableCellWithIdentifier:normalCellReuseIdentifier];

        if (cell == nil) {
            NSArray* topLevelObjects = [[NSBundle mainBundle] loadNibNamed:@"ANormalCell" owner:nil options:nil];
            cell = (ANormalCell*)[topLevelObjects objectAtIndex:0];
        }
    } else {
        cell = (ASearchCell*)[self.tableView dequeueReusableCellWithIdentifier:searchCellReuseIdentifier];

        if (cell == nil) {
            NSArray* topLevelObjects = [[NSBundle mainBundle] loadNibNamed:@"ASearchCell" owner:nil options:nil];
            cell = (ASearchCell*)[topLevelObjects objectAtIndex:0];
        }
    }

    return cell;
}

很有可能。 所有tableView數據源方法都使用tableView參數調用。 如果

if (tableView == searchController.searchResultsTableView)

然后通過添加子視圖或自定義屬性來更改單元格。 在這種情況下,與其他任何方法一樣,自定義單元也采用相同的方法。

是的,您可以設置cell.imageView,cell.accessoryView,cell.BackGroundView等。

閱讀此UITableViewCell文檔以獲取更多信息

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM