第二个 UIViewController 上的 UITableView

Posted

技术标签:

【中文标题】第二个 UIViewController 上的 UITableView【英文标题】:UITableView on a second UIViewController 【发布时间】:2013-08-05 11:49:24 【问题描述】:

我看到了许多其他类似的问题,但我没有找到(正确的)答案。我在两个不同的UIViewControllers 上有两个UITableViews。但是第二个UITableView 没有显示任何行。

第二个 UIViewController 的代码(detail.m):

//  detail.m    

#import "detail.h"

@interface detail ()

@end

@implementation detail 


@synthesize tweetsArray;
@synthesize tableView;
@synthesize searchBar;

- (void)viewDidLoad

    [super viewDidLoad];
    //1
    self.tableView.dataSource = self;
    //2
    self.tweetsArray = [[NSArray alloc] initWithObjects:
                        @"Always put your fears behind you and your dreams in front of you.",
                        @"A relationship with no trust is like a cell phone with no service, all you can do is play games.",
                        @"People should stop talking about their problem and start thinking about the solution.",
                        @"Dear Chuck Norris, Screw you. I can grill burgers under water. Sincerely, Spongebob Squarepants.",
                        @"My arms will always be open for you, they will never close, not unless you're in them.",
                        nil];


//3</pre>
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section 
    return [self.tweetsArray count];


//4
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
    //5
    static NSString *cellIdentifier = @"SettingsCell2";

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];

    //5.1 you do not need this if you have set SettingsCell as identifier in the storyboard (else you can remove the comments on this code)
    //if (cell == nil)
    //    
    //        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:cellIdentifier];
    //   

    //6
    NSString *tweet = [self.tweetsArray objectAtIndex:indexPath.row];
    //7
    [cell.textLabel setText:tweet];
    [cell.detailTextLabel setText:@"via Codigator"];
    return cell;


- (void)didReceiveMemoryWarning

    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
    

@end

【问题讨论】:

NSLog self.tweetsArray countnumberOfRowsInSection。看看你的数组里有没有内容? 尝试在你的 cellForRowAtIndexPath 中放置一个断点。 函数不会执行 O.o.我添加了一个断点,但它并没有停止。 检查tableview的delegate和datasource是否连接正确 我忘了连接一些东西 THX :) :) :) :) :) :) 【参考方案1】:

我没有看到您为 tableview 设置委托。你需要设置它

self.tableView.delegate = self;

【讨论】:

以上是关于第二个 UIViewController 上的 UITableView的主要内容,如果未能解决你的问题,请参考以下文章

在单视图应用程序中添加第二个 UIViewController

在 UINavigationController 中运行 UIViewController 作为 UISplitViewController 的第二个视图

呈现 UINavigationController 并在导航栏上方显示附加(第二个)标题

如何从一个 XIB 中的不同 UIViewController 文件控制第二个 UIView(子视图)

iOS9:UIViewController:当 viewController 在第二个 UIWindow 中运行时,不会在拆分视图更改时调用 viewWillTransitionToSize

从另一个 UIViewController 呈现 UIViewController