按下 UITableViewCell 时做一些事情

Posted

技术标签:

【中文标题】按下 UITableViewCell 时做一些事情【英文标题】:Do some thing when press UITableViewCell 【发布时间】:2016-10-15 23:43:27 【问题描述】:

我在UIViewController 中创建了表格视图,并成功创建了单元格,但是当按下任何单元格时我不知道该怎么做。我想如果按一个单元格,转到另一个视图控制器。我添加了一些方法,但没有奏效。我应该添加哪些方法以及如何将方法与操作联系起来?

此方法用于创建数组

-(void)addCell

    int wholeNumber = 500;
    int leftOver = wholeNumber%90;
    int sectionNumber = wholeNumber / 90;;

    tableTitle = [[NSMutableArray alloc] init];
    tableSubtitle = [[NSMutableArray alloc] init];

    for (int i =1; i<= sectionNumber; i++) 
        if (i == 1) 
            NSString *stringForTitle = [NSString stringWithFormat:@"%d",i];
            NSString *stringForsubitle = [NSString stringWithFormat:@"90 Questions. Free."];
            [tableTitle addObject:stringForTitle];
            [tableSubtitle addObject:stringForsubitle];


        else if (i==2)
            NSString *stringForTitle = [NSString stringWithFormat:@"%d",i];

            NSString *stringForsubitle = [NSString stringWithFormat:@"90 Questions. Unlock or Rate App."];
            [tableTitle addObject:stringForTitle];
            [tableSubtitle addObject:stringForsubitle];
        else if (i==3)
            NSString *stringForTitle = [NSString stringWithFormat:@"%d",i];

            NSString *stringForsubitle = [NSString stringWithFormat:@"90 Questions. Unlock or Sign in and rate 15 questions."];
            [tableTitle addObject:stringForTitle];
            [tableSubtitle addObject:stringForsubitle];
        else if (i==4)
            NSString *stringForTitle = [NSString stringWithFormat:@"%d",i];

            NSString *stringForsubitle = [NSString stringWithFormat:@"90 Questions. Unlock or Sign in and write explanation for 4 questions."];
            [tableTitle addObject:stringForTitle];
            [tableSubtitle addObject:stringForsubitle];
        else if (i>4)
            NSString *stringForTitle = [NSString stringWithFormat:@"%d",i];

            NSString *stringForsubitle = [NSString stringWithFormat:@"90 Questions. Unlock or Sign in and contribute 2 questions."];
            [tableTitle addObject:stringForTitle];
            [tableSubtitle addObject:stringForsubitle];
        else

        




    
    if (leftOver == 0) 


     else 
        if (sectionNumber == 0) 
            NSString *stringForTitle = [NSString stringWithFormat:@"1"];

            NSString *stringForsubitle = [NSString stringWithFormat:@"%d Questions. Free.",leftOver];
            [tableTitle addObject:stringForTitle];
            [tableSubtitle addObject:stringForsubitle];
        else if (sectionNumber==1)
            NSString *stringForTitle = [NSString stringWithFormat:@"2"];

            NSString *stringForsubitle = [NSString stringWithFormat:@"%d Questions. Unlock or Rate app.",leftOver];
            [tableTitle addObject:stringForTitle];
            [tableSubtitle addObject:stringForsubitle];
        else if (sectionNumber==2)
            NSString *stringForTitle = [NSString stringWithFormat:@"3"];

            NSString *stringForsubitle = [NSString stringWithFormat:@"%d Questions. Unlock or Sign in and rate 15 questions.",leftOver];
            [tableTitle addObject:stringForTitle];
            [tableSubtitle addObject:stringForsubitle];
        else if (sectionNumber==3)
            NSString *stringForTitle = [NSString stringWithFormat:@"4"];

            NSString *stringForsubitle = [NSString stringWithFormat:@"%d Questions. Unlock or Sign in and write explanation for 3 questions.",leftOver];
            [tableTitle addObject:stringForTitle];
            [tableSubtitle addObject:stringForsubitle];
        else if (sectionNumber>3)
            NSString *stringForTitle = [NSString stringWithFormat:@"%d",sectionNumber +1];

            NSString *stringForsubitle = [NSString stringWithFormat:@"%d Questions. Unlock or Sign in and contribute 1 questions.",leftOver];
            [tableTitle addObject:stringForTitle];
            [tableSubtitle addObject:stringForsubitle];
        else 

        

    

    NSString *stringForTitle = [NSString stringWithFormat:@"CQ"];
    NSString *stringForsubitle = [NSString stringWithFormat:@"Contribute Questions."];
    [tableTitle addObject:stringForTitle];
    [tableSubtitle addObject:stringForsubitle];






- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView

    return 1;


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

    return [tableTitle count];

此方法创建单元格

-(UITableViewCell *)tableView:(UITableView *)_tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"thisCell"];



    UILabel *titleLable= [[UILabel alloc] initWithFrame:CGRectMake(0, 4, 60, 40)];
    titleLable.text = [tableTitle objectAtIndex:indexPath.row];

    [titleLable setFont:[UIFont systemFontOfSize:25]];
    titleLable.backgroundColor = [UIColor whiteColor];
    titleLable.textAlignment = NSTextAlignmentCenter;
    CALayer* layer = [titleLable layer];
    CALayer *bottomBorder = [CALayer layer];
    bottomBorder.borderColor = [UIColor lightGrayColor].CGColor;
    bottomBorder.borderWidth = 1;
    bottomBorder.frame = CGRectMake(-1, layer.frame.size.height-1, layer.frame.size.width, 1);
    [bottomBorder setBorderColor:[UIColor lightGrayColor].CGColor];
    [layer addSublayer:bottomBorder];

    [cell.contentView addSubview:titleLable];
    CGFloat width = [UIScreen mainScreen].bounds.size.width;
    CGFloat actualwidth = width - 100;


    UILabel *subtitleLable = [[UILabel alloc] initWithFrame:CGRectMake(60, 4, actualwidth, 40)];
    subtitleLable.text = [tableSubtitle objectAtIndex:indexPath.row];
    subtitleLable.numberOfLines = 0;

    [subtitleLable setFont:[UIFont systemFontOfSize:10]];
    subtitleLable.backgroundColor = [UIColor whiteColor];


    CALayer* layer2 = [subtitleLable layer];
    CALayer *bottomBorder2 = [CALayer layer];
    bottomBorder2.borderColor = [UIColor lightGrayColor].CGColor;
    bottomBorder2.borderWidth = 1;
    bottomBorder2.frame = CGRectMake(-1, layer2.frame.size.height-1, layer2.frame.size.width, 1);
    [bottomBorder2 setBorderColor:[UIColor lightGrayColor].CGColor];
    [layer2 addSublayer:bottomBorder2];



    [cell.contentView addSubview:subtitleLable];


    UILabel *spaceLable = [[UILabel alloc] initWithFrame:CGRectMake(actualwidth+60, 4, 100, 40)];

    spaceLable.text = @"";
    spaceLable.numberOfLines = 0;

    [spaceLable setFont:[UIFont systemFontOfSize:11]];
    spaceLable.backgroundColor = [UIColor whiteColor];


    CALayer* layer3 = [spaceLable layer];
    CALayer *bottomBorder3 = [CALayer layer];
    bottomBorder3.borderColor = [UIColor lightGrayColor].CGColor;
    bottomBorder3.borderWidth = 1;
    bottomBorder3.frame = CGRectMake(-1, layer3.frame.size.height-1, layer3.frame.size.width, 1);
    [bottomBorder3 setBorderColor:[UIColor lightGrayColor].CGColor];
    [layer3 addSublayer:bottomBorder3];


    [cell.contentView addSubview:spaceLable];












    return cell;



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

    rowNo = indexPath.row;

这是表格视图的图像

这是表视图委托连接图像。

【问题讨论】:

【参考方案1】:

我可以看到你已经写了 UITableViewDelegate 方法

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

    rowNo = indexPath.row;

这个委托应该可以工作,但我认为您可能忘记为您的 UITableView 设置委托。

tableView.delegate = self

如果您使用 Interface builder 创建了 tableView,请检查 UITableView 委托连接

【讨论】:

非常感谢。我应该在哪里添加tableView.delegate = self?我已经将委托添加到 UITableView,这就是单元格显示的原因。这是图像的链接。 1drv.ms/i/s!AqDPUF7IFADqg7oGBWgX3k5XH8CAyA 无论您在哪里创建 tableView,都可以在 tableView.datasource=self 旁边添加这一行 'tableView.delegate=self' 我使用情节提要创建了这个 tableView。并使用故事板连接委托和数据源。我真的不知道哪里出了问题。 我的错,当你点击单元格时请检查,你是否在- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath方法中获得了控制权,如果你在那里获得了控制权,通过放置一个断点然后你可以继续您的新 viewController 或您想以任何方式移至 NewViewController - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath NSLog(@"a"); rowNo = indexPath.row; (rowN == 0) NSLog(@"a"); 。我放了这个,但无法通过单击单元格来控制方法。【参考方案2】:

有一些选项可以做到这一点。

    您已经拥有 tableview 本身的委托。像这样设置委托:

    self.tableView.delegate = self;
    

现在当用户选择任何单元格时实现您的委托。

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

   int ClickedRow = indexPath.row;
  // You can go further with this ID->ClickedRow 

 

    您可以将 UIButton 添加到 tablecell。

     - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
     
    
      UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"thisCell"];
    
    
      UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
    
      //set the position of the button
     button.frame = CGRectMake(cell.frame.origin.x + 100, cell.frame.origin.y + 20, 100, 30);
     [button setTitle:@"myButton" forState:UIControlStateNormal];   
      [button addTarget:self action:@selector(customActionPressed:) forControlEvents:UIControlEventTouchUpInside];
      button.backgroundColor= [UIColor clearColor];
      [cell addSubview:button];
    
     return cell;
     
    
    
      -(void)customActionPressed:(UIButton*)sender
      
    
       NSLog(@"%i",[sender tag]); / with this tag, you can do anything..
    
      
    

    您可以添加手势:在 cellForRowAtIndexPath 中,执行此操作。您在表格单元上有一个图像视图或按钮。所以给它添加标签:

         UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]    initWithTarget:self action:@selector(handleImageTap:)];
        tap.cancelsTouchesInView = YES;
        tap.numberOfTapsRequired = 1;
        imageView.tag = indexPath.row;
    
        [imageView addGestureRecognizer:tap];
    
    
       // handle method
     - (void) handleImageTap:(UIGestureRecognizer *)gestureRecognizer 
      
         //Do anything here ....
    
    
         NSLog (@"%d",[gestureRecognizer.view tag]);
    
    
          
    

【讨论】:

谢谢,成功了。我使用了第三个,使用手势识别器。你如何识别点击了哪个单元格? - (void) handleImageTap:(UIGestureRecognizer *)gestureRecognizer NSLog(@"a"); if (rowNo == 0) NSLog(@"0"); if (rowNo == 1) NSLog(@"1"); 我想添加这个,那么如何识别我点击的是哪个单元格?换句话说,如何在这里制作rowNo 向单元格添加 uiimageview 并向其添加手势。更新了答案。检查它-***.com/questions/7712161/… 很高兴知道。继续。

以上是关于按下 UITableViewCell 时做一些事情的主要内容,如果未能解决你的问题,请参考以下文章

在显示 contextMenu 时做一些事情

当所有 alamofire 请求完成时做一些事情

当音源播放 2 分钟时做一些事情

如何在退出 Qt 应用程序时做一些事情

PHP Javascript?当用户关闭页面或浏览器时做一些事情[重复]

当本地通知到达或超过可以使用的通知总数时做一些事情