【问题标题】:Xcode didSelectRowAtIndexPath on tableview not called未调用 tableview 上的 Xcode didSelectRowAtIndexPath
【发布时间】:2012-09-30 13:09:25
【问题描述】:

我一直在寻找今天早上 6 点,但我无法弄清楚为什么没有调用 didSelectRowAtIndexPath 方法。我对此感到非常绝望。

tableview 显示正确,但我无法成功启用单元格选择。

在头文件中,我添加了:

@interface AlertsTable : UIViewController<UITableViewDelegate, UITableViewDataSource, CMPopTipViewDelegate>{
    UITableView *TableView;
}

@property (nonatomic, retain)   UITableView *TableView;

在实现文件中:

@synthesize TableView;

- (void)viewDidLoad
{
    [super viewDidLoad];

    // Uncomment the following line to preserve selection between presentations.
    // self.clearsSelectionOnViewWillAppear = NO;

    // Uncomment the following line to display an Edit button in the navigation bar for this view controller.
    // self.navigationItem.rightBarButtonItem = self.editButtonItem;


    //Set the title


    //Format Alerts table
    //self.view.backgroundColor = [UIColor redColor];
    CGFloat sideMargin = 10;
    CGFloat topBottomMargin = 44;
    CGFloat originX = sideMargin;
    // compute width based on view size
    CGFloat sizeWidth = (self.view.bounds.size.width - (sideMargin * 2));
    CGFloat originY = topBottomMargin;
    // compute height based on view size
    CGFloat sizeHeight = (self.view.bounds.size.height - (topBottomMargin * 2));
    //self.view.frame = CGRectMake(originX, originY, sizeWidth, sizeHeight);

    self.TableView = [[UITableView alloc] initWithFrame:CGRectMake(originX, originY, sizeWidth, sizeHeight) style:UITableViewStylePlain];


    //Initialize the array.
    AlertsItems = [[NSMutableArray alloc] initWithObjects: @"Alert 1", @"Alert 2", @"Alert 3" , @"Alert 4", @"Alert 5", @"Alert 6", nil];

[self.TableView setDelegate:self];
[self.TableView setDataSource:self];
[self.view addSubview:TableView];
TableView.userInteractionEnabled = YES;
TableView.allowsSelection = YES;
TableView.allowsSelectionDuringEditing = YES;
NSLog(@"delegate:%@ dataSource:%@", self.TableView.delegate, self.TableView.dataSource);

}

委托和数据源在检查中都不为零。
请注意,“Alertstable”继承自 UIViewController,而不是 UITableViewController。
由于我选择的实现,这是必要的:表格视图显示在屏幕上显示的弹出窗口上(使用我从互联网上获取的另一个类)。

这是 didSelectRowAtIndexPath 方法:

#pragma mark - Table view delegate

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSString *alertString = [NSString stringWithFormat:@"Clicked on row #%d", [indexPath row]];
    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:alertString message:@"" delegate:self cancelButtonTitle:@"Done" otherButtonTitles:nil];
    [alert show];

}

方法:

[super touchesBegan:...];  
[super touchesEnded:...];  
[super touchesMoved:...];  

未实施。

我从另一个 ViewController 添加了 AlertTable

AlertsTable *AlertTable = [[AlertsTable alloc] WithButton:sender withArray:self.PopTipViews];
[self.view addSubview:AlertTable.view];

我也实现了:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"Cell";


    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];

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

    // Set up the cell...
    [[cell textLabel] setText:[AlertsItems objectAtIndex:indexPath.row]];
    NSLog (@"%@",[AlertsItems objectAtIndex:indexPath.row]);
    return cell;

}

您知道问题可能是什么吗?
我在 Xcode 方面不是很有经验,而且我真的被卡住了。

编辑:

我知道它不是自定义的,但是可以在这里找到项目的链接(也许我忽略了一些东西:

https://www.bilbu.be/BarConnect_v2.zip

LoginViewController.xib 没有正确链接(它在 en.lproj 子文件夹中可用)所以你可能需要先链接(我注意到太晚了,我之前有朋友上传了文件 - 我无法重新上传不幸的是我自己)。
ViewController VC 类调用AlertsTable VC 类。我想您可以忽略该项目中的更多内容...
请注意,这个项目的目的只是作为可视化界面原型。它并不意味着是最佳优化的应用程序(这是其他开发人员会做的)。如果这不起作用,我将使用 Photoshop 来创建界面,但我想这不会那么有说服力......

【问题讨论】:

  • 你分配并初始化你的 TableView 了吗?
  • 数据源是否正确实现? (例如 cellForRowAtIndexPath :) 另一个问题:你在使用故事板吗?
  • 您好,是的,我已分配并初始化。我还实现了 cellForRowAtIndexPath。我将编辑我的原始帖子,并添加此代码。如下所述,我会尽快上传Xcode项目的路径,但要等到朋友发给我链接。
  • 我不使用情节提要。抱歉,回复晚了。
  • 谢谢,该项目的链接是bilbu.be/BarConnect_v2.zip LoginViewController.xib 未正确链接,因此您可能需要这样做,我注意到为时已晚。 ViewController VC 类调用AlertsTable VC 类。我想您可以忽略项目中的更多内容...

标签: xcode uitableview uiviewcontroller didselectrowatindexpath


【解决方案1】:

我成功编译了您的项目。问题是你违背了 MVC 模式。控制器 (AlertsTable) 位于视图 (CMPopTipView) 内。我建议您重新考虑控制器和视图的层次结构。希望这会对你有所帮助。

【讨论】:

  • +1 因为这确实帮助了我。正如我所提到的,我是 Xcode 开发的新手,所以在阅读了 MVC 后,我立即明白我走错了路。我将清楚地将视图与控制器分开,并从头开始重建。谢谢!
  • 没问题。如果您发现我的回答很好,请考虑接受它。谢谢!
  • 我将改变视图/控制器的结构,看看我能否在这个周末让它工作。我宁愿把这篇文章留到周末之后,以防我得到一些额外的反馈。但如果事实证明这是我的问题的根源,我会接受你的回答。谢谢
  • 好的,谢谢!顺便说一句,请注意 Apple 的 UIPopoverController(类似于您的 CMPopTipView)是控制器,而不是视图。
  • 感谢您的额外提示。事实上,我认为我会选择 UIPopoverController。显然,我将这个类的范围扩展得太远了。
【解决方案2】:

尝试改变

TableView.userInteractionEnabled = YES;
TableView.allowsSelection = YES;
TableView.allowsSelectionDuringEditing = YES;

self.TableView.userInteractionEnabled = YES;
self.TableView.allowsSelection = YES;
self.TableView.allowsSelectionDuringEditing = YES;

你是在分配 self.TableView 吗?比如……

self.TableView = [[UITableView alloc] init];//[[UITableView alloc] initWithStyle:UITableViewStylePlain];

【讨论】:

  • 你好,我晚上会回答,因为我有一个项目会议。谢谢你,我会检查的。
  • 您好,我进行了更改。我应用的代码是: self.TableView = [[UITableView alloc] initWithFrame:CGRectMake(originX, originY, sizeWidth, sizeHeight) style:UITableViewStylePlain];不幸的是,结果相同。我会尽快上传 Xcode 文件并提供链接,但我必须请朋友上传。感谢您的帮助。
  • 你有没有把任何东西加载到你的tableView中?我想如果你没有,那可能是你的问题......
  • 您好,是的,我添加了一个包含测试项目的数组...不幸的是,这不起作用。
  • 我还有一个想法……这个构建是完全以编程方式还是在 IB 中并已连接?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-02-15
  • 2010-09-20
  • 1970-01-01
相关资源
最近更新 更多