按UITableViewCell

时间:2016-10-15 23:43:27

标签: ios objective-c xcode uitableview

我在UIViewController创建了表格视图,并成功创建了单元格,但在按任何单元格时我无法弄清楚如何做某事。我想按下一个单元格,转到另一个视图控制器。我添加了一些方法,但没有用。我应该添加哪些方法以及如何将该方法与操作相关联?

此方法适用于create Array

-(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;
}

这是表格视图的图像形式

enter image description here

这是表格视图委托连接图像。enter image description here

2 个答案:

答案 0 :(得分:1)

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

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    rowNo = indexPath.row;
}

这个代理应该可以工作,但我想您可能忘记为UITableView设置代理。

tableView.delegate = self

如果已使用“接口”构建器创建了tableView,则检查UITableView委托连接

答案 1 :(得分:0)

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

  1. 您已经拥有了tableview本身的代表。将代理设置为:

    self.tableView.delegate = self;
    
  2. 现在,当用户选择任何单元格时,实现您的委托。

       - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
     {
    
       int ClickedRow = indexPath.row;
      // You can go further with this ID->ClickedRow 
    
     }
    
    1. 您可以将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..
      
        }
      
    2. 您可以添加手势:在cellForRowAtIndexPath中,执行此操作。您在tablecell上有一个imageview或按钮。所以添加标签:

           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]);
      
      
            }