سؤال

أنا باستخدام شريط التبويب (UITabBarController) على التطبيق و أتمنى أن تخصيص مظهر من الجدول الذي يظهر عند النقر فوق زر المزيد.لقد عملت كيفية تغيير مظهر شريط التنقل على أكثر من شاشة من خلال وضع

self.moreNavigationController.navigationBar.barStyle

في فئة فرعية من UITabBarController و لقد تمكنت من تغيير لون خلفية الجدول عن طريق تعديل

self.moreNavigationController.topViewController.view.backgroundColor

, لكن لا أستطيع العمل على كيفية تغيير لون الخط في الخلايا التي تظهر على الطاولة.كنت أتمنى أن استخدام

self.moreNavigationController.topViewController.view.visibleCells 

ولكن هذا يبدو دائما أن يكون فارغا.لقد حاولت القيام بذلك في viewDidLoad ، viewWillAppear و viewDidAppear مع أي نجاح.الكائن الذاتي.moreNavigationController.topViewController هو من نوع UIMoreListController التي يبدو أن تكون غير موثقة و لا أستطيع أن أرى أي شيء واضح في واجهة من شأنها أن تساعد لي.

أي أفكار ؟

هل كانت مفيدة؟

المحلول

ويتم ملؤها visibleCells فقط بعد عرض moreNavigationController.

ويتم إنشاء خلايا في وقت التشغيل، وذلك حتى إذا قمت بتغيير محتوى الخلايا، يتم استبدالها عند عرضها.

وشيء واحد في محاولة سيكون لاستبدال مصدر بيانات من moreNavigationController tableView، استدعاء cellForRowAtIndexPath من مصدر البيانات الأصلي وتغيير محتواه قبل إعادته.

<ع> استخدام التعليمات البرمجية أدناه، بعد أن عرض مرة واحدة في moreNavigationController تهيئة ذلك، سترى أنه عند العودة إلى moreNavigationController، والخلايا الحمراء، ولكن العودة فورا إلى خلفية بيضاء.

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

نصائح أخرى

وبعد يوم من اقتراح ستيفان لاستبدال مصدر بيانات من moreNavigationController، وهنا هو سريع جهة نظر أكثر من رمز I تنفيذها.

وأنا خلق فئة جديدة تسمى 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 I تجاوز 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;
}

شكرا غير معروف.بعد حل له, سوف أضع الكود في سويفت.فقط ما يجب أن تفعل أكثر من ذلك هو خلق MoreTableViewCell في الدرجة فقط.لم يكن لديك إلى استخدام القصة المصورة.إذا كنت ترغب في تعديل tableView يمكنك أن تفعل ذلك في customizeMoreTableView الأسلوب.

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)
}

 }

وتابعت تنفيذ إيان لتخصيص المزيد من القائمة، ولكن كنت أعاني من مشكلة الاحتفاظ التخصيصات بعد تحذير الذاكرة. يبدو didReceiveMemoryWarning لتدمير UITableView، وعندما يتم إعادة انها تحصل على مصدر البيانات القديم إلى الوراء. وهنا قال لي الحل:

وأنا استبدال viewDidLoad على CustomTabBarController مع هذا:

- (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];
    }
}

وكما ترون، وأضفت بعض الخصائص لتخزين الأشياء احتاجه. هذه يجب أن تضاف إلى رأس وتوليفها. كما تقدمت CustomTabBarController على 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، في كل مرة لقد أظهرت ذلك.

@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
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top