2009-01-13 18 views
19

पर अधिक मेनू को कस्टमाइज़ करना मैं अपने ऐप पर एक टैब बार (UITabBarController) का उपयोग कर रहा हूं और जब आप अधिक बटन क्लिक करते हैं तो तालिका की उपस्थिति को कस्टमाइज़ करना चाहता हूं। मैं बाहर काम किया है नेविगेशन बार UITabBarController के उपवर्ग मेंटैब बार

self.moreNavigationController.navigationBar.barStyle 

सेट कर और अधिक स्क्रीन पर है इस बात का स्वरूप बदलने के लिए कैसे और मैं

संशोधित करके तालिका के पृष्ठभूमि का रंग बदलने में कामयाब रहे
self.moreNavigationController.topViewController.view.backgroundColor 

, लेकिन मैं तालिका पर दिखाई देने वाली कोशिकाओं में फ़ॉन्ट रंग को बदलने का तरीका नहीं समझ सकता। मैं मैं

self.moreNavigationController.topViewController.view.visibleCells 

इस्तेमाल कर सकते हैं उम्मीद कर रहा था, लेकिन यह हमेशा खाली हो रहा है। मैंने इसे देखने में प्रयास करने की कोशिश की है, कोई भी सफलता के साथ देखें, देखें WillAppear और viewDidAppear। ऑब्जेक्ट self.moreNavigationController.topViewController प्रकार UIMoreListController है, जो अनियंत्रित प्रतीत होता है और मैं इंटरफ़ेस में कुछ भी स्पष्ट नहीं देख सकता जो मेरी मदद करेगा।

कोई विचार?

उत्तर

19

दृश्यकेल अधिक नवाइंटर नियंत्रक प्रदर्शित होने के बाद ही पॉप्युलेट किया जाता है।

और कोशिकाओं को रनटाइम पर बनाया जाता है, इसलिए यदि आप कोशिकाओं की सामग्री को बदलते हैं, तो उन्हें प्रदर्शित होने पर प्रतिस्थापित किया जाता है।

कोशिश करने की एक चीज़ अधिक नवाइंटर कंट्रोलर तालिका के डेटासोर्स को प्रतिस्थापित करने के लिए होगी, मूल डेटासोर्स के सेलफॉररोएटइंडेक्सपैथ को कॉल करें और इसे वापस करने से पहले अपनी सामग्री को बदलें।

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

UITableView *view = (UITableView *)self.tabBarController.moreNavigationController.topViewController.view; 
if ([[view subviews] count]) { 
    for (UITableViewCell *cell in [view visibleCells]) { 
    cell.backgroundColor = [UIColor redColor]; 
    } 
} 
+0

अच्छा सुझाव। मैं इसे कोशिश करने जा रहा हूं – Ian1971

+0

यह काम करता है। मैं इस साइट पर नया हूं, क्या मुझे अपने विचार के कार्यान्वयन को एक अलग उत्तर के रूप में पोस्ट करना चाहिए? सही प्रोटोकॉल क्या है? – Ian1971

+0

मैं आपके स्वयं के प्रश्न को संपादित करने और नीचे दिए गए उत्तर को जोड़ने का सुझाव दूंगा। लेकिन मैं भी एक नौसिखिया हूँ! –

26

moreNavigationController के डेटा स्रोत को बदलने के लिए स्टीफ़न का सुझाव से जारी रखते हुए, यहाँ कोड मैं कार्यान्वित की दृष्टि से अधिक एक त्वरित है।

मैंने MoreTableViewDataSource नामक एक नई कक्षा बनाई है जो UITableViewDataSource प्रोटोकॉल लागू करता है। नियंत्रक जो अधिक पृष्ठ वास्तव में तालिका बनाने के लिए उपयोग करता है उसे UIMoreListControllerModern कहा जाता है, और यह UITableViewDataSource प्रोटोकॉल के केवल आवश्यक हिस्सों को लागू करता है। मेरा कार्यान्वयन इस तरह दिखता है।

-(MoreTableViewDataSource *) initWithDataSource:(id<UITableViewDataSource>) dataSource 
{ 
    self = [super init]; 
    if (self) 
    { 
      self.originalDataSource = dataSource; 
    } 

    return self; 
} 

- (void)dealloc 
{ 
    self.originalDataSource = nil; 
    [super dealloc]; 
} 

- (NSInteger)tableView:(UITableView *)table numberOfRowsInSection:(NSInteger)section 
{ 
    return [originalDataSource tableView:table numberOfRowsInSection:section]; 
} 


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    UITableViewCell *cell = [originalDataSource tableView:tableView cellForRowAtIndexPath:indexPath]; 
    cell.textColor = [UIColor whiteColor]; 
    return cell; 
} 

और फिर मेरी CustomTabBarController कक्षा में इस प्रकार मैं viewDidLoad ओवरराइड:

- (void)viewDidLoad { 

    [super viewDidLoad]; 

    UINavigationController *moreController = self.moreNavigationController; 
    moreController.navigationBar.barStyle = UIBarStyleBlackOpaque; 

    if ([moreController.topViewController.view isKindOfClass:[UITableView class]]) 
    { 

     UITableView *view = (UITableView *)moreController.topViewController.view; 
     view.backgroundColor = [UIColor blackColor]; 
     moreTableViewDataSource = [[MoreTableViewDataSource alloc] initWithDataSource:view.dataSource]; 
     view.dataSource = moreTableViewDataSource; 

    } 
} 

के रूप में यहाँ का अनुरोध हेडर फाइल

@interface MoreTableViewDataSource : NSObject <UITableViewDataSource> 
{ 
    id<UITableViewDataSource> originalDataSource; 
} 

@property (retain) id<UITableViewDataSource> originalDataSource; 

-(MoreTableViewDataSource *) initWithDataSource:(id<UITableViewDataSource>) dataSource; 

@end 

और

#import "MoreTableViewDataSource.h" 

@interface CustomTabBarController : UITabBarController 
{ 
    MoreTableViewDataSource *moreTableViewDataSource; 
} 
+0

बस बातचीत में शामिल हो रहा है ... अगर मैं सही ढंग से समझता हूं, तो आप अधिक TableViewDataSource में चेन कर रहे हैं। अच्छा! प्रश्न हालांकि: क्या आवंटन को संतुलित करने के लिए view.dataSource असाइनमेंट के बाद [moreTableViewDataSource रिलीज़] होना चाहिए? –

+0

आह, एक सेकंड प्रतीक्षा करें। अधिक TableViewDataSource को किसी प्रॉपर्टी के माध्यम से एक्सेस नहीं किया जाता है, इसलिए कोई अतिरिक्त बरकरार नहीं है, है ना? लेकिन मुझे आश्चर्य है कि यह एक संपत्ति होनी चाहिए ... हम्म ... –

+0

मैंने इसे स्वयं और आईओएस 3.एक्स पर कुछ लागू किया है। मैंने देखा है कि टेक्स्ट लेबल का पृष्ठभूमि रंग सेट नहीं करता है। टेबलव्यू की पृष्ठभूमि के रूप में यह हमेशा एक ही रंग होता है। –

2

मैं पीछा कर रहे हैं इयान का इम्प्ला अधिक मेनू को कस्टमाइज़ करने के लिए उत्सर्जन, लेकिन मुझे स्मृति चेतावनी के बाद अनुकूलन को बनाए रखने में कोई समस्या हो रही थी। didReceiveMemoryWarning UITableView को नष्ट करने लगता है, और जब इसे पुन: उत्पन्न किया जाता है तो उसे पुराना डेटा स्रोत वापस मिल जाता है।यहाँ मेरी समाधान है:

मैं इस के साथ CustomTabBarController पर viewDidLoad बदल देते हैं:

- (void)viewDidLoad { 
    [super viewDidLoad]; 
    UINavigationController* moreController = self.moreNavigationController; 
    if ([moreController.topViewController.view isKindOfClass:[UITableView class]]) { 
     moreController.delegate = self; 
     self.moreControllerClass = [moreController.topViewController class]; 
     UITableView* view = (UITableView*) moreController.topViewController.view; 
     self.newDataSource = [[[MoreDataSource alloc] initWithDataSource:view.dataSource] autorelease]; 
    } 
} 

आप देख सकते हैं, मैं चीजों को मैं जरूरत के भंडारण के लिए कुछ गुण गयी। उन्हें हेडर में जोड़ा जाना चाहिए और संश्लेषित करना होगा। मैंने कस्टमटाबबार नियंत्रक को हेडर में एक UINavigationControllerDelegate भी बनाया है। यहाँ प्रतिनिधि समारोह मैं जोड़ा है:

- (void)navigationController:(UINavigationController *)navigationController willShowViewController:(UIViewController *)viewController animated:(BOOL)animated { 
    if ([viewController isKindOfClass:self.moreControllerClass]) { 
     UIView* view = self.moreNavigationController.topViewController.view; 
     if ([view isKindOfClass:[UITableView class]]) { 
      UITableView* tview = (UITableView*) view; 
      tview.dataSource = self.newDataSource; 
      tview.rowHeight = 81.0; 
     } 
    } 
} 

इस तरह से मैं यकीन है कि मेरे कस्टम डेटा स्रोत हमेशा की तरह, प्रयोग किया जाता है क्योंकि मैं इसे इस तरह से सिर्फ UIMoreListController, हर बार यह दिखाया गया है दिखा करने से पहले सेट कर सकते हैं।

1
@interface TabBarViewController() <UITableViewDelegate,UITableViewDataSource> 

@property (nonatomic,strong) UITableView* tabBarTableView; 
@property (nonatomic,weak) id <UITableViewDelegate> currentTableViewDelegate; 

@end 

@implementation TabBarViewController 

- (void)viewDidLoad { 
    [super viewDidLoad]; 
    [self costumizeMoreTableView]; 

} 

-(void)costumizeMoreTableView{ 
    _tabBarTableView = (UITableView *)self.moreNavigationController.topViewController.view; 
    _currentTableViewDelegate = _tabBarTableView.delegate; 
    _tabBarTableView.delegate = self; 
    _tabBarTableView.dataSource = self; 
    [_tabBarTableView registerNib:[UINib nibWithNibName:@"MoreTabBarTableViewCell" bundle:nil] forCellReuseIdentifier:@"MoreTabBarTableViewCell"]; 

} 

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{ 
    return 120; 
} 

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{ 
    return 2; 
} 

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{ 
    MoreTabBarTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"MoreTabBarTableViewCell" forIndexPath:indexPath]; 
    [cell setMoreTableValues]; 
    return cell; 
} 

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath: (NSIndexPath *)indexPath{ 
     [_currentTableViewDelegate tableView:tableView didSelectRowAtIndexPath:indexPath]; 
} 

@end 
3

अज्ञात के लिए धन्यवाद। अपने समाधान के बाद, मैं स्विफ्ट में अपना कोड डालूंगा। केवल आपको और क्या करना चाहिए अधिकTableViewCell क्लास और बस इसे बनाना है। आपको स्टोरीबोर्ड का उपयोग करने की ज़रूरत नहीं है। यदि आप tableView को संशोधित करना चाहते हैं तो आप इसे CustomMableTiewView विधि में कर सकते हैं।

class TabBarMenuController: UITabBarController, UITableViewDelegate, UITableViewDataSource{ 

var tabBarItems: [UIViewController] = [] 
var areMessagesVisible: Bool = false 

var titleForTabBars: [String] = ["resources", "events", "training", "my profile", "news", "contacts"] 
var iconNames: [String] = ["film", "calendar", "classroom", "profile", "news", "Phone"] 
var controllersStoryboardId: [String] = ["resourcesNavController", "eventsNavController", "enablementNavController", "profileNavController", "newsNavController", "contactsNavController"] 

// to manage moreTableView 
var moreTableView: UITableView = UITableView() 
var currentTableViewDelegate: UITableViewDelegate? 

override func viewDidLoad() { 
    super.viewDidLoad() 

    self.customizeMoreTableView() 
    //to REMOVE 
    areMessagesVisible = true 

    if !areMessagesVisible{ 
     self.titleForTabBars.removeAtIndex(4) 
     self.controllersStoryboardId.removeAtIndex(4) 
     self.iconNames.removeAtIndex(4) 
    } 

    for i in 0 ..< controllersStoryboardId.count{ 
     tabBarItems.append(UIStoryboard(name: "Main", bundle: NSBundle.mainBundle()).instantiateViewControllerWithIdentifier(controllersStoryboardId[i]) as? UINavigationController ?? UINavigationController()) 
    } 
    self.moreNavigationController.navigationBar.tintColor = UIColor.blackColor() 
} 

override func viewWillAppear(animated: Bool) { 

    for i in 0 ..< tabBarItems.count{ 
     tabBarItems[i].tabBarItem = UITabBarItem(title: titleForTabBars[i], image: UIImage(named: iconNames[i]), selectedImage: UIImage(named: iconNames[i])) 
    } 
    self.viewControllers = tabBarItems 
} 

func customizeMoreTableView(){ 
    moreTableView = self.moreNavigationController.topViewController!.view as? UITableView ?? UITableView() 
    currentTableViewDelegate = moreTableView.delegate; 
    moreTableView.delegate = self 
    moreTableView.dataSource = self; 
    moreTableView.registerClass(MoreTableViewCell.self, forCellReuseIdentifier: "MoreTableViewCell") 

} 

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell { 

    let moreCell = tableView.dequeueReusableCellWithIdentifier("MoreTableViewCell", forIndexPath: indexPath) as? MoreTableViewCell ?? MoreTableViewCell() 

    moreCell.textLabel?.text = titleForTabBars[indexPath.row + 4] 
    moreCell.imageView?.image = UIImage(named: iconNames[indexPath.row + 4]) 

    /*let testLabel: UILabel = UILabel(frame: CGRect(x: 0, y: 0, width: 100, height: 40)) 
    testLabel.backgroundColor = UIColor.yellowColor() 
    moreCell.addSubview(testLabel) 
    */ 
    return moreCell 
} 
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int { 
    return titleForTabBars.count - 4 
} 

func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) { 
    currentTableViewDelegate?.tableView!(tableView, didSelectRowAtIndexPath: indexPath) 
} 

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