【问题标题】:iphone development: using tableview cells as a buttoniphone开发:使用tableview单元格作为按钮
【发布时间】:2012-09-08 21:28:03
【问题描述】:

是否可以像按钮一样使用 tableView 单元格?在我的应用程序中,我想要一个有 4 行的表格视图,并且在每一行中都会有标签。我想要的是当我触摸一行时,表格 tableview 单元格应该像按钮一样做出反应(执行操作并突出显示背景)?那可能吗?我怎样才能做到这一点?谢谢。

【问题讨论】:

    标签: iphone objective-c ios uitableview uibutton


    【解决方案1】:

    非常简单的委托方法将被称为 didSelectRowAtIndexPath 在那里你可以得到哪一行在哪个部分被点击,你可以执行进一步的操作。

    例如

    -(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
        // deselect
        [tableView deselectRowAtIndexPath:indexPath animated:YES];
    
        if(indexPath.row == 0){
            LoginFormViewController *vController = nil;
            if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad)
            {
                vController = [[LoginFormViewController alloc] initWithNibName:@"LoginFormViewController_ipad" bundle:nil];
            }else {
                vController = [[LoginFormViewController alloc] initWithNibName:@"LoginFormViewController" bundle:nil];
            }
            [self.navigationController pushViewController:vController animated:YES];
            [vController release];
        }
    }
    

    【讨论】:

      【解决方案2】:

      您可以在 UITableView 委托中的 tableView:didSelectRowAtIndexpath 方法中实现此“按钮”功能;)

      - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath 
      {
          // do something when pressed on an UITableViewCell
      }
      

      【讨论】:

        【解决方案3】:

        cel 将突出显示,并且您的 table view 的委托将收到tableView:didSelectRowAtIndexPath: 消息。你不需要任何额外的东西来实现你所描述的。

        【讨论】:

        • 改变高亮颜色怎么样?
        • 您有灰色或蓝色作为选项,除此之外您可以制作图像,并使用 UIImageView 作为单元格的selectedBackgroundView
        【解决方案4】:
        - (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] autorelease];
               cell.selectionStyle=UITableViewCellSelectionStyleGray;
        
            }
        
        
        
            if(tableView.tag==1)
            {
            cell.userInteractionEnabled = NO;
            cell.backgroundColor =[UIColor blackColor];
        
            UIButton *Btn11=[UIButton buttonWithType:UIButtonTypeCustom];
            [Btn11 addTarget:self action:@selector(ratingAction:) forControlEvents:UIControlEventTouchUpInside];
            [Btn11 setBackgroundImage:[UIImage imageNamed:@"small_img4.png"] forState:UIControlStateNormal];
            Btn11.tag=indexPath.row;
            Btn11.frame=CGRectMake(0, 6.5, 156, 74.5);
        
        
                UILabel *Main_tblImg_Label=[[UILabel alloc]initWithFrame:CGRectMake(0, 80,156, 31)];
                [Main_tblImg_Label setBackgroundColor:[UIColor blackColor]];
                //[Main_tblImg_Label setText:@"First Drive 2012 Mercedes Benz M class"];
                //Main_tblImg_Label.textAlignment = UITextAlignmentCenter;
                [Main_tblImg_Label setTextColor:[UIColor whiteColor]];
                [Main_tblImg_Label setFont:[UIFont fontWithName:@"Helvetica" size:9]];
                [Main_tblImg_Label setNumberOfLines:2];
        
                [cell.contentView addSubview:Main_tblImg_Label];
                [cell.contentView addSubview:Btn11];
        
        
        
                UILabel *other_Lbl=[[UILabel alloc]initWithFrame:CGRectMake(14, 80, 142, 29)];
                [other_Lbl setText:@"First Drive 2012 \nMercedes Benz M class"];
                [other_Lbl setBackgroundColor:[UIColor blackColor]];
                [other_Lbl setTextColor:[UIColor whiteColor]];
                [other_Lbl setFont:[UIFont fontWithName:@"Helvetica" size:9]];
                [other_Lbl setNumberOfLines:2];
                [cell.contentView addSubview:other_Lbl];
                [cell.contentView addSubview:Btn11];
                        //[cell.contentView addSubview:Main_tblImg_Label];
        //      [cell.contentView addSubview:Btn11];
        //      
        
        
        
        
        
        
        
            UIButton *Btn22=[UIButton buttonWithType:UIButtonTypeCustom];
            [Btn22 addTarget:self action:@selector(ratingAction2:) forControlEvents:UIControlEventTouchUpInside];
            [Btn22 setBackgroundImage:[UIImage imageNamed:@"Screen2.png"] forState:UIControlStateNormal];
            Btn22.tag=indexPath.row;
            Btn22.backgroundColor=[UIColor clearColor];
            Btn22.frame=CGRectMake(165, 6.5, 156, 74.5);
        
                UILabel *main_tblImg_Lbl2=[[UILabel alloc]initWithFrame:CGRectMake(165, 80, 156, 31)];
        
                [main_tblImg_Lbl2 setBackgroundColor:[UIColor blackColor]];
                //[main_tblImg_Lbl2 setText:@"Audi planning Mexican Assembly plant?"];
                [main_tblImg_Lbl2 setTextColor:[UIColor whiteColor]];
                //[main_tblImg_Lbl2 setFont:[UIFont fontWithName:@"times" size:1]];
                [main_tblImg_Lbl2 setFont:[UIFont fontWithName:@"Helvetica" size:10]];
                [main_tblImg_Lbl2 setNumberOfLines:2];
                [cell.contentView addSubview:main_tblImg_Lbl2];
        
        
                UILabel *other1_Lbl=[[UILabel alloc]initWithFrame:CGRectMake(176, 80, 136, 31)];
                [other1_Lbl setText:@"Audi planning Mexican Assembly plant?"];
                [other1_Lbl setBackgroundColor:[UIColor blackColor]];
                [other1_Lbl setTextColor:[UIColor whiteColor]];
                [other1_Lbl setFont:[UIFont fontWithName:@"Helvetica" size:10]];
                [other1_Lbl setNumberOfLines:2];
                [cell.contentView addSubview:other1_Lbl];
                [cell.contentView addSubview:Btn11];
        
        
            [cell.contentView addSubview:Btn22];
            }
        return cell
        }
        

        【讨论】:

          猜你喜欢
          • 2023-03-08
          • 1970-01-01
          • 2019-09-02
          • 1970-01-01
          • 1970-01-01
          • 2020-10-20
          • 1970-01-01
          • 1970-01-01
          • 2013-05-10
          相关资源
          最近更新 更多