Xcode didSelectRowAtIndexPath 在 tableview 上没有被调用
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/12661471/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Xcode didSelectRowAtIndexPath on tableview not called
提问by Trace
I have been looking until 6 o'clock this morning, but I can't figure out why the didSelectRowAtIndexPath method is not being called. I am getting quite desparate on this one.
我一直在寻找今天早上 6 点,但我无法弄清楚为什么没有调用 didSelectRowAtIndexPath 方法。我对这个很绝望。
The tableview is shown properly, but I cannot succeed to enable the selection of a cell.
tableview 显示正确,但我无法成功启用单元格的选择。
In the header file , I added:
在头文件中,我添加了:
@interface AlertsTable : UIViewController<UITableViewDelegate, UITableViewDataSource, CMPopTipViewDelegate>{
UITableView *TableView;
}
@property (nonatomic, retain) UITableView *TableView;
In the implementation file:
在实现文件中:
@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);
}
The delegate and datasource are both not nil on the check.
Note that the "Alertstable" inherits from a UIViewController, not a UITableViewController.
This is necessary due to the implementation I chose: the tableview is shown on a popupwindow shown on the screen (using another class that I took from the internet).
委托和数据源在支票上都不为零。
请注意,“Alertstable”继承自 UIViewController,而不是 UITableViewController。
由于我选择的实现,这是必要的:tableview 显示在屏幕上显示的弹出窗口上(使用我从互联网上获取的另一个类)。
This is the didSelectRowAtIndexPath method:
这是 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];
}
The methods:
方法:
[super touchesBegan:...];
[super touchesEnded:...];
[super touchesMoved:...];
are not implemented.
未实施。
I added the AlertTable from another ViewController
我从另一个 ViewController 添加了 AlertTable
AlertsTable *AlertTable = [[AlertsTable alloc] WithButton:sender withArray:self.PopTipViews];
[self.view addSubview:AlertTable.view];
I also implemented:
我还实施了:
- (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;
}
Do you have any idea what the problem could be?
I'm not very experienced in Xcode and I'm really stuck.
你知道可能是什么问题吗?
我对 Xcode 不是很有经验,而且我真的被困住了。
Edit:
编辑:
I know that it is not custom, but the link to the project can be found here (perhaps I'm overlooking something:
我知道这不是自定义的,但是可以在此处找到该项目的链接(也许我忽略了一些东西:
https://www.bilbu.be/BarConnect_v2.zip
https://www.bilbu.be/BarConnect_v2.zip
The LoginViewController.xib is not properly linked (it is available in the en.lproj subfolder) so you may need to link first (I noticed too late and I had the file uploaded by a friend before - I cannot re-upload it myself unfortunately).
ViewController VC class calls the AlertsTable VC class. There is more in the project that I suppose you can ignore...
Note that the purpose of this project is only to serve as visual interface prototype. It is not meant to be the best optimized application (this is what other developers will do). If this doesn't work, I will use Photoshop to create the interfaces, but I guess that's not gonna be as convincing...
LoginViewController.xib 未正确链接(它在 en.lproj 子文件夹中可用),因此您可能需要先链接(我注意到为时已晚,而且我之前已由朋友上传该文件 - 不幸的是我无法自己重新上传它)。
ViewController VC 类调用AlertsTable VC 类。项目中还有更多我想您可以忽略的内容......
请注意,该项目的目的只是作为可视化界面原型。它并不意味着是最佳优化的应用程序(这是其他开发人员会做的)。如果这不起作用,我将使用 Photoshop 创建界面,但我想这不会令人信服......
回答by Rudolf Adamkovi?
I successfully compiled your project. The problem is that you're going against the MVC pattern. The controller (AlertsTable) is inside of a view (CMPopTipView). I'd suggest you to rethink the hierarchy of the controllers and views. Hope this will help you.
我成功编译了你的项目。问题是你违背了 MVC 模式。控制器 ( AlertsTable) 位于视图 ( CMPopTipView) 内。我建议您重新考虑控制器和视图的层次结构。希望这会帮助你。
回答by Rich Fox
Try changing
尝试改变
TableView.userInteractionEnabled = YES;
TableView.allowsSelection = YES;
TableView.allowsSelectionDuringEditing = YES;
to
到
self.TableView.userInteractionEnabled = YES;
self.TableView.allowsSelection = YES;
self.TableView.allowsSelectionDuringEditing = YES;
and are you allocating self.TableView? Something like...
你在分配 self.TableView 吗?就像是...
self.TableView = [[UITableView alloc] init];//[[UITableView alloc] initWithStyle:UITableViewStylePlain];

