UItableView选择问题

时间:2015-12-19 11:42:50

标签: ios objective-c uitableview

我创建了一个名为HomeTable的tableview。我设置了委托和数据源,但是 didselectrowatindexpath 方法没有调用。贝洛是我的code.i创建的hometablecell,这也是正确的,但我不知道我做了什么错误。请帮助我out.thank you

 -(void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath
    {
        if ([cell respondsToSelector:@selector(setSeparatorInset:)]) {
            [cell setSeparatorInset:UIEdgeInsetsZero];
        }

        if ([cell respondsToSelector:@selector(setLayoutMargins:)]) {
            [cell setLayoutMargins:UIEdgeInsetsZero];
        }
    }

    -(void)viewDidLayoutSubviews
    {
        if ([HomeTable respondsToSelector:@selector(setSeparatorInset:)]) {
            [HomeTable setSeparatorInset:UIEdgeInsetsZero];
        }

        if ([HomeTable respondsToSelector:@selector(setLayoutMargins:)]) {
            [HomeTable setLayoutMargins:UIEdgeInsetsZero];
        }

    }

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

        return 1;
    }

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

        if (RestName.count==0) {
            return 1;
        }
        return RestName.count;
    }


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

        static NSString *simpleTableIdentifier = @"Cell";

        HomeCell *cell = (HomeCell *)[tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier];
        if (cell == nil)
        {
            NSArray *nibArray = [[NSBundle mainBundle] loadNibNamed:@"HomeCell" owner:self options:nil];
            cell = [nibArray objectAtIndex:0];
        }

        NSDictionary *ResDiction = [RestName objectAtIndex:indexPath.row];
        cell.ResLabel.font=[UIFont fontWithName:Bask size:16];
        cell.LabelRes.font=[UIFont fontWithName:Bask size:16];
        cell.ResLabel.text = [ResDiction objectForKey:@"business_name"];
        cell.LabelRes.text= [ResDiction objectForKey:@"business_category"];
        cell.ResLabel.adjustsFontSizeToFitWidth=YES;
        cell.LabelRes.adjustsFontSizeToFitWidth=YES;

        return cell;

    }

    -(void)tableView:(UITableView *)tableView didDeselectRowAtIndexPath:(NSIndexPath *)indexPath{

        if (indexPath.row==0) {

            UIStoryboard *Story=[UIStoryboard storyboardWithName:@"Main" bundle:nil];
            Beverages *sachin=[Story instantiateViewControllerWithIdentifier:@"Bever"];
            [self.navigationController pushViewController:sachin animated:YES];
        }else{

            UIAlertView*alert=[[UIAlertView alloc]initWithTitle:@"Hello" message:@"Selected" delegate:self cancelButtonTitle:@"Cool" otherButtonTitles:nil, nil];
            [alert show];
        }

    }

2 个答案:

答案 0 :(得分:2)

Ans是你的方法错了

您需要使用didselectrowatindexpath方法

而不是didDeselectRowAtIndexPath方法。

答案 1 :(得分:1)

这是代码应该是什么样子:

-(void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath
    {
        if ([cell respondsToSelector:@selector(setSeparatorInset:)]) {
            [cell setSeparatorInset:UIEdgeInsetsZero];
        }

        if ([cell respondsToSelector:@selector(setLayoutMargins:)]) {
            [cell setLayoutMargins:UIEdgeInsetsZero];
        }
    }

    -(void)viewDidLayoutSubviews
    {
        if ([HomeTable respondsToSelector:@selector(setSeparatorInset:)]) {
            [HomeTable setSeparatorInset:UIEdgeInsetsZero];
        }

        if ([HomeTable respondsToSelector:@selector(setLayoutMargins:)]) {
            [HomeTable setLayoutMargins:UIEdgeInsetsZero];
        }

    }

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

        return 1;
    }

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

        if (RestName.count==0) {
            return 1;
        }
        return RestName.count;
    }


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

        static NSString *simpleTableIdentifier = @"Cell";

        HomeCell *cell = (HomeCell *)[tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier];
        if (cell == nil)
        {
            NSArray *nibArray = [[NSBundle mainBundle] loadNibNamed:@"HomeCell" owner:self options:nil];
            cell = [nibArray objectAtIndex:0];
        }

        NSDictionary *ResDiction = [RestName objectAtIndex:indexPath.row];
        cell.ResLabel.font=[UIFont fontWithName:Bask size:16];
        cell.LabelRes.font=[UIFont fontWithName:Bask size:16];
        cell.ResLabel.text = [ResDiction objectForKey:@"business_name"];
        cell.LabelRes.text= [ResDiction objectForKey:@"business_category"];
        cell.ResLabel.adjustsFontSizeToFitWidth=YES;
        cell.LabelRes.adjustsFontSizeToFitWidth=YES;

        return cell;

    }

    -(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{

        if (indexPath.row==0) {

            UIStoryboard *Story=[UIStoryboard storyboardWithName:@"Main" bundle:nil];
            Beverages *sachin=[Story instantiateViewControllerWithIdentifier:@"Bever"];
            [self.navigationController pushViewController:sachin animated:YES];
        }else{

            UIAlertView*alert=[[UIAlertView alloc]initWithTitle:@"Hello" message:@"Selected" delegate:self cancelButtonTitle:@"Cool" otherButtonTitles:nil, nil];
            [alert show];
        }

    }

另外请确保启用了UITableview(如果来自XIB文件)单元格选择。