【问题标题】:How to change properties of a TableView in a UIViewController如何在 UIViewController 中更改 TableView 的属性
【发布时间】:2012-03-06 14:26:36
【问题描述】:

我是 iOS 开发的初学者,需要一些帮助:

我有一个带有 2 个嵌套 TableView 的 UIViewController。 (类似here的实现)

.h 文件

#import <UIKit/UIKit.h>

@interface TabAboutViewController : UIViewController <UITableViewDelegate, UITableViewDataSource> {
}

    @property (nonatomic, strong) NSArray *navigationList;

@end

.m 文件

#import "TabAboutViewController.h"

@interface TabAboutViewController ()

@end

@implementation TabAboutViewController

#pragma mark -
#pragma mark Synthesize

@synthesize navigationList;
@synthesize informationTableView;

#pragma mark -
#pragma mark View Load

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    navigationList = [NSArray arrayWithObjects:@"Was leistet die App?",@"Wie wird gemessen?",@"Wie wird ausgewertet?",@"Was sind die Vorteile?",nil];
}

#pragma mark -
#pragma mark View UnLoad

- (void)viewDidUnload
{
    [super viewDidUnload];
    // Release any retained subviews of the main view.

}

#pragma mark -
#pragma mark View Rotates

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    return YES;
}

#pragma mark -
#pragma mark Table Management

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    // Return the number of sections.
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    // Return the number of rows in the section.
    return [self.navigationList count];
}

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

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"aboutCell"];
    NSString *cellText = [self.navigationList objectAtIndex:indexPath.row];
    cell.textLabel.text = cellText;

    return cell;
}

@end

现在我想为 UIViewController 中的一个 TableViews 设置背景颜色,但我不知道如何以及在哪里进行。我不想在情节提要中更改它。

【问题讨论】:

    标签: ios xcode uitableview uiview interface-builder


    【解决方案1】:

    你可以在-viewDidLoad

    - (void)viewDidLoad
    {
        [super viewDidLoad];
        // Do any additional setup after loading the view, typically from a nib.
        self.myTableView.backgroundColor = [UIColor red];
        // ...
    }
    

    如果您还没有这样做,请不要忘记在 .h 文件中的表视图中添加 IBOutlet 并从 Interface Builder 链接到它:

    @property (weak, nonatomic) IBOutlet UITableView *myTableView;
    

    如果你使用标签来识别你的视图,你可以这样做:

    - (void)viewDidLoad
    {
        [super viewDidLoad];
        // Do any additional setup after loading the view, typically from a nib.
        UITableView *myTableView = [self.view viewWithTag:1]; // Or what ever tag you associate with the table view
        myTableView.backgroundColor = [UIColor red];
        // ...
    }
    

    【讨论】:

    • 嗯,我测试了这两种方法...但是背景颜色没有改变
    • 您确定将表格视图链接到 IBOutlet 还是设置了正确的标记值?当您尝试访问它时,您确定myTableView 不是nil 吗?尝试在viewDidLoad 末尾添加NSLog(@"myTableView: %@", myTableView);
    • 出于测试目的,我删除了第二个表,然后将 (@property (weak, nonatomic) IBOutlet UITableView *mytableView;) 添加到 .h 文件, (@synthesize mytableView;) 添加到 .m文件和 (self.mytableView.backgroundColor = [UIColor blackColor];) 到 viewDidLoad 方法... NSLog(@"myTableView: %@", myTableView);回馈“null”
    • 好的,上面的答案找到了我的问题:我忘了在 interfaceBuilder 中连接插座...正如我所说 - 我对 ios dev 完全陌生;)
    【解决方案2】:
    - (void)viewDidLoad
    {
        self.tableview.backgroundcolor = [UIColor blueColor]; // change color here..
    
    
        [super viewDidLoad];
        // Do any additional setup after loading the view, typically from a nib.
        navigationList = [NSArray arrayWithObjects:@"Was leistet die App?",@"Wie wird gemessen?",@"Wie wird ausgewertet?",@"Was sind die Vorteile?",nil];
    }
    

    【讨论】:

      【解决方案3】:

      首先,您可以在 nib 文件中定义背景颜色!

      但是,如果您想基于代码进行操作,请确保已将界面生成器中的表格视图连接到 .h 文件中相应的 IBOutlets。然后,您可以从您想要的任何位置(viewDidLoad 或更高版本)访问代码中的这些表视图。

      【讨论】:

      • omg,谢谢 - 我忘了在 interfacebuilder 中连接插座;)
      猜你喜欢
      • 1970-01-01
      • 2017-03-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-02-15
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多