我在Xcode 4.6中有一个应用程序,它使用故事板.我在视图控制器类中添加了一个UITableView,该类按预期工作.然而,当我试图删除情节提要中的UITableView并以编程方式将其添加回同一个类时,我遇到了两个具体问题:

1) Although I set the UITableViewCell type to be of type subtitle, the detail label no longer appears.

2) The segue that should occur when I select a cell does not occur, and prepare segue is not even being called, indicating that no message is being sent to the table view when a cell is selected.

以下是相关代码:

@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;
}

至于segue,它不能被调用,因为你不能对你用代码而不是IB创建的表进行segue.同样,回到老方法,使用tableView:DidSelectRowatineXpath: Select 单元格时会调用它.在那里实例化你的细节控制器,并用代码进行转换.

编辑后:

我没有看到你添加的代码.您已经实现了DidSelectRowatineXpath,而不是DidSelectRowatineXpath.如果你改变了这一点,你的segue应该可以工作.

Objective-c相关问答推荐

iOS - 如何预加载键盘?

如何使 UIView 动画序列重复和自动反转

UIButton 标题更改为默认值

tableView:canEditRowAtIndexPath: 弹出 viewController 时崩溃

在objective-c中isa是什么意思?

#if 和 #ifdef Objective-C 预处理器宏有什么区别?

将长格式的 NSString 拆分为多行

`-fembed-bitcode` 和 BITCODE_GENERATION_MODE 有什么区别?

如何初始化 NSString 的数组?

如何知道 NSAssert 在发布版本中是否被禁用?

如何在 iOS 中拨打电话?

.delegate=self 是什么意思?

在 Xcode 5 中运行代码覆盖时出现数十个profiling:invalid arc tag

如何从 AVPlayer(不是 AVAudioPlayer)获取持续时间?

Obj-C 中的多值枚举

在我的 iOS Mail 和 Safari 应用程序中支持 Open In... 菜单项

NSSet 到 NSArray 转换调用 objectAtIndex?

Objective-C 中的 super 到底是什么?

两个 NSDate 对象之间的区别 - 结果也是一个 NSDate

UiTextField 中的第一个字母小写