开发者

TableView Row unable to Select

开发者 https://www.devze.com 2023-02-14 14:15 出处:网络
I defined my own controller with no nib file like this: @interface EngineViewController : UIViewController <UITableViewDelegate, UITableViewDataSource> {

I defined my own controller with no nib file like this:

@interface EngineViewController : UIViewController <UITableViewDelegate, UITableViewDataSource> {
    EngineViewProperties* viewProp;
    UIImageView *imgView; // image view of selected engine
    NSUInteger selectedIndex;
    UITableView *menu;
}

@property (nonatomic,retain) E开发者_如何学JAVAngineViewProperties* viewProp;

- (EngineViewController *) initWithEngineViewProperties: (EngineViewProperties *) _viewProp;

- (void) dropdownMenu: (id) sender;

I created my view in loadView,with three subviews. The subview arrowBtn is helped to popup a list of search engines.

- (void)loadView {
    // ...
    UIButton *arrowBtn = [[UIButton alloc] initWithFrame:rect];
    [arrowBtn setImage:viewProp.arrowImg forState:UIControlStateNormal];

    [arrowBtn addTarget:self action:@selector(dropdownMenu:) forControlEvents:UIControlEventTouchUpInside];

    [self.view addSubview:imgView];
    [self.view addSubview:label];
    [self.view addSubview:arrowBtn];
    // ...
}

I create a table listing search engines in the selector dropdownMenu:

- (void) dropdownMenu: (id) sender {
    UIButton *arrowBtn = (UIButton *)sender;
    // ...
    menu = [[UITableView alloc] initWithFrame:rect style:UITableViewStylePlain];
    menu.delegate = self;
    menu.dataSource = self;
    menu.backgroundColor = [UIColor blackColor];
    menu.allowsSelection = YES;
    [self.view addSubview:menu];
    [self.view bringSubviewToFront:menu];
    [menu release];
}

And I implemented

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath

as usual. But the result is that the menu popup happily,but I can do nothing with the cells.I clicked the cells,but no response.Those methods like "didSelectRowAtIndexPath" can not be called. Sorry to paste up so much codes one time.But I really need help.I don't know what is the problem.Please forgive me for my poor English and low development skill in Iphone.And thanks a lot if you give me a little suggestions.

//Added-------------------------

"numberOfRowsInSection" is simple:

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return [viewProp.txtArray count];
}

and another method:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    static NSString *MenuItems = @"MenuItems";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:MenuItems];
    if (cell == nil) {
            cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier: MenuItems] autorelease];
    }
    NSUInteger row = [indexPath row];

    cell.imageView.image = [viewProp.imgArray objectAtIndex:row];
    cell.textLabel.text = [viewProp.txtArray objectAtIndex:row];
    cell.textLabel.textColor = [UIColor whiteColor];
    cell.textLabel.font = [UIFont systemFontOfSize:12.0];
    cell.selectionStyle = UITableViewCellSelectionStyleNone; // Blue style tried,helpless too
    if (row == selectedIndex) {
            cell.selected = YES;
    }
    cell.userInteractionEnabled = YES;
    return cell;
}

// Added

Strangely,when the menu firstly poped up,the default selected cell was highlighted correctly,but very quickly the highlighting disappeard.


Check if you have implemented this

UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]
                                   initWithTarget:self
                                   action:@selector(dismissKeyboard)];

[self.view addGestureRecognizer:tap];

The didSelectRowAtIndexPath will not called if this is present. One way to workaround this is set [self.view addGestureRecognizer:tap]; towards your targeted view only such as [self.svContent addGestureRecognizer:tap];.

Alternatively, check which view is touched.

- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch
{
    if ((touch.view == YourTable)) 
    {
        return NO;
    }
    return YES;
}


Try giving cell.selectionStyle = UITableViewCellSelectionStyleBlue in your table View data source. Also check if the userInteraction is enabled for the table view and the cells.


Are you showing any data in the table have you implemented

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {

in you viewController??

Seems that you are setting the delgate property correctly, also you added the protocols to the same ViewController. So the problem can be either there is no data in the tableView for selection, or there is some View on top of tableView which is blocking you interaction with the tableView.


Use

cell.selectionStyle = UITableViewCellSelectionStyleGray;

in your

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath

for your cell.

remove

cell.userInteractionEnabled = YES;

code from your file.

0

精彩评论

暂无评论...
验证码 换一张
取 消