ios - 以编程方式创建 UITableView

标签 ios objective-c xcode uitableview

我在 Xcode 4.6 中有一个使用 Storyboard 的应用程序。我向 View Controller 类添加了一个 UITableView,它按预期工作。但是,当我尝试删除 Storyboard 中的 UITableView 并以编程方式将其添加回同一类时,我遇到了两个具体问题:

1)虽然我将UITableViewCell类型设置为subtitle类型,但是detail label不再出现了。

2)我选择单元格时应该发生的segue没有发生,甚至没有调用prepare segue,表明选择单元格时没有向表格 View 发送消息。

以下是相关代码:

@interface StatsTableViewController () <UITableViewDataSource, UITableViewDelegate>
@property (strong, nonatomic) UITableView *tableView;

@end

@implementation StatsTableViewController

-(UITableView *)makeTableView
{
    CGFloat x = 0;
    CGFloat y = 50;
    CGFloat width = self.view.frame.size.width;
    CGFloat height = self.view.frame.size.height - 50;
    CGRect tableFrame = CGRectMake(x, y, width, height);

    UITableView *tableView = [[UITableView alloc]initWithFrame:tableFrame style:UITableViewStylePlain];

    tableView.rowHeight = 45;
    tableView.sectionFooterHeight = 22;
    tableView.sectionHeaderHeight = 22;
    tableView.scrollEnabled = YES;
    tableView.showsVerticalScrollIndicator = YES;
    tableView.userInteractionEnabled = YES;
    tableView.bounces = YES;

    tableView.delegate = self;
    tableView.dataSource = self;

    return tableView;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    self.tableView = [self makeTableView];
    [self.tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:@"newFriendCell"];
    [self.view addSubview:self.tableView];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"newFriendCell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];

    if (cell == nil) 
    {
        cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier];
    }

    Friend *friend = [self.fetchedResultsController objectAtIndexPath:indexPath];

     **//THIS DATA APPEARS**
    cell.textLabel.text = friend.name;
    cell.textLabel.font = [cell.textLabel.font fontWithSize:20];
    cell.imageView.image = [UIImage imageNamed:@"icon57x57"];

    **//THIS DATA DOES NOT APPEAR**
    cell.detailTextLabel.text = [NSString stringWithFormat:@"%i Games", friend.gameCount];
    cell.detailTextLabel.textColor = [UIColor lightGrayColor];

    return cell;
}

 -(void)tableView:(UITableView *)tableView didDeselectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [self performSegueWithIdentifier:@"detailsView" sender:self];
}

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
      //I set the segue identifier in the interface builder
     if ([segue.identifier isEqualToString:@"detailsView"])
     {

         NSLog(@"segue"); //check to see if method is called, it is NOT called upon cell touch

         NSIndexPath *indexPath = [self.tableView indexPathForCell:sender];
         ///more code to prepare next view controller....
     }
}

我不确定自己忘记做什么才能解决这两个问题。任何帮助表示赞赏。

最佳答案

当您注册一个类并使用 dequeueReusableCellWithIdentifier:forIndexPath: 时,dequeue 方法保证返回一个单元格,因此永远不会输入您的 if (cell == nil) 子句。所以,就照旧做,不注册类,使用dequeueReusableCellWithIdentifier:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"newFriendCell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];

    if (cell == nil) {
        cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier];
    }
//etc.
return cell;
}

至于转场,它不能被调用,因为你不能对你在代码中创建的表进行转场,而不是在 IB 中。再次,回到旧的方式并使用 tableView:didSelectRowAtIndexPath: 当你选择一个单元格时将被调用。在那里实例化您的细节 Controller 并在代码中进行转换。

修改后:

我没有在那里看到您添加的代码。您已经实现了 didDeselectRowAtIndexPath 而不是 didSelectRowAtIndexPath。如果你改变它,你的 segue 应该可以工作。

关于ios - 以编程方式创建 UITableView,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15846707/

相关文章:

ios - SwiftyJSON 不重新加载 UITableView

ios - 核心文本中的垂直文本对齐

ios - UIButton 的 IBAction 导致无法识别的选择器发送到实例错误(iOS)

iphone - 需要帮助在 UIView 中正确定位 UIView

ios - 无法搜索数据?

ios - 归档与构建

xcode - FFMPEG 二进制文件抛出错误 : Undefined Symbols for Architecture x86_64

objective-c - 从 NSUserDefaults 中的数组中删除对象

javascript - onPlay(或 onClick?)在 iPad 上触发全屏视频

iOS - 如何从推送的 Controller 更改根导航 Controller 的属性