我在 Xcode 4.6 中有一个使用故事板的应用程序。我将 UITableView 添加到视图控制器类中,它按预期工作。然而,当我尝试删除故事板中的 UITableView 并以编程方式将其添加回同一个类时,我遇到了两个具体问题:
1)虽然我将UITableViewCell类型设置为subtitle类型,但是详细标签不再出现。
2)当我选择一个单元格时应该发生的segue没有发生,并且甚至没有调用prepare segue,这表明当选择一个单元格时没有消息被发送到表视图。
这是相关代码:
@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: 时,出队方法保证返回一个单元格,因此永远不会输入 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:didSelectRowAtIndexPath: 当您选择一个单元格时将调用它。在那里实例化您的详细控制器并在代码中进行转换。
编辑后:
我在那里没有看到您添加的代码。您已经实现了 didDeselectRowAtIndexPath 而不是 didSelectRowAtIndexPath。如果你改变它,你的segue应该可以工作。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)