0

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

表格视图显示正确,但我无法成功启用单元格的选择。

在头文件中,我添加了:

@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 创建界面,但我想这不会那么令人信服……

4

2 回答 2

2

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

于 2012-10-04T21:37:45.060 回答
0

尝试改变

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];
于 2012-09-30T13:41:14.150 回答