gpt4 book ai didi

ios - 以编程方式创建 UITableView

转载 作者:IT老高 更新时间:2023-10-28 11:38:33 24 4
gpt4 key购买 nike

我在 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/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com