2010-11-14 14 views
5

मैं अपने TableView पर SearchBar जोड़ना चाहता हूं। मैंने आईबी में UITableView के शीर्षलेख पर UISearchBar खींच लिया, और यह मेरे UITableView के साथ स्क्रॉल किया गया।UITearchBar UITableViewController में?

मैं अब UITableViewController उपयोग करने के लिए बदल गया है, और जब मैं UITableView जो UITableViewController साथ प्रदान की जाती है के शीर्षक में एक UISearchBar खींचते हैं, यह बिल्कुल दिखाई नहीं देता।

क्या कोई चाल है?

तरह का संबंध

उत्तर

19

आप इसे प्रोग्राम के रूप में

UISearchBar *tempSearchBar = [[UISearchBar alloc] initWithFrame:CGRectMake(0, 0, self.tableView.frame.size.width, 0)]; 
self.searchBar = tempSearchBar; 
[tempSearchBar release]; 
self.searchBar.delegate = self; 
[self.searchBar sizeToFit]; 
self.tableView.tableHeaderView = self.searchBar; 

आशा इस मदद करता है कर सकते हैं।

+0

ग्रेट समाधान। एक खोजबार जोड़ने के लिए, मुझे पहले UIViewController का उपयोग करना पड़ा। अब हमारे समाधान के साथ, मैं UITableViewController का उपयोग कर सकता हूं। –

4

जब भी मैं निब में खोजबार को परिभाषित करता हूं, तो मैं इसे उद्देश्य पर तालिका दृश्य के शीर्षलेख में नहीं जोड़ता। इसके बजाय मैंने इसेडिडलोड फ़ंक्शन में सेट किया है। Retterdesdialogs समाधान भी काम करता है, इसलिए सुनिश्चित नहीं है कि उसे और वोट क्यों नहीं मिला है। बंडल विधि यह अभी भी दिखाई देना चाहिए:

- (void)viewDidLoad 
{ 
    [super viewDidLoad]; 
    tableView.tableHeaderView = searchBar; 
} 
1

यह भी कैसे tableview नियंत्रक प्रारंभ करने के लिए, यदि आप alloc और initWithNib के माध्यम से init पर निर्भर करता है। लेकिन टेबलव्यू नियंत्रक के साथ मुझे लगता है कि डिफ़ॉल्ट कार्यान्वयन विधि initWithStyle है। अगर आपने initWithStyle के साथ आवंटित किया है और nib फ़ाइल कभी लोड नहीं होगी।

0

मैंने अभी कोशिश की और यह मेरे लिए काम किया, एक चाल नहीं होनी चाहिए। यदि आप कहीं भी अपनी परियोजना का एक डिस्टिल्ड संस्करण पोस्ट करना चाहते हैं तो मैं आपको देखने में मदद कर सकता हूं।

7

मैं इसे दो UITableViewDelegate प्रोटोकॉल विधियों का उपयोग करके काम करने के लिए प्राप्त करता हूं-tableView: viewForHeaderInSection: और -tableView: heightForHeaderInSection: जैसा कि नीचे दिया गया है।

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{ 
    if (section == 0) { 
     UISearchBar *tempSearchBar = [[UISearchBar alloc] initWithFrame:CGRectMake(0, 0, self.tableView.frame.size.width, 0)]; 
     [tempSearchBar sizeToFit]; 
     return tempSearchBar; 
    } 
    return [UIView new]; 
} 

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{ 
    if (section == 0) { 
     return 40.0f; 
    } 
    return 0.1f; 
} 

उम्मीद है कि यह मदद करता है।

+1

इससे मदद मिली। धन्यवाद। –

+0

धन्यवाद .... कूल :) –

संबंधित मुद्दे