xcode 按钮操作上的 uitableview 单元格选择

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14314312/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-15 02:31:09  来源:igfitidea点击:

uitableview cell selection on button action

objective-cxcodeuitableviewuibutton

提问by user1845209

My table view uses a custom class to create cells

我的表视图使用自定义类来创建单元格

MyCustomCell.h

MyCustomCell.h

#import <UIKit/UIKit.h>

@interface MyCustomCell : UITableViewCell

@end

MyCustomCell.m

MyCustomCell.m

#import "MyCustomCell.h"
#import <QuartzCore/QuartzCore.h>


@implementation MyCustomCell

- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self)
    {
        // Initialization code

    }
    return self;
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated
{
    [super setSelected:selected animated:animated];

    self.selectionStyle = UITableViewCellSelectionStyleBlue;

    // Configure the view for the selected state
}

- (void)layoutSubviews
{
    [super layoutSubviews];
    self.imageView.frame = CGRectMake(5,4,80,80);
    self.imageView.layer.masksToBounds = YES;
    self.imageView.layer.cornerRadius = 5.0f;
    self.imageView.contentMode = UIViewContentModeScaleAspectFill;
    float imgWidth =  self.imageView.image.size.width;
    if(imgWidth > 0)
    {
        self.textLabel.frame = CGRectMake(110,2,170,25);
        self.detailTextLabel.frame = CGRectMake(110,18,200,25);
        self.detailTextLabel.font = [UIFont boldSystemFontOfSize:12];
        self.detailTextLabel.alpha = 0.0;
        self.textLabel.font = [UIFont boldSystemFontOfSize:12];
        self.textLabel.textColor = [UIColor colorWithRed:.2314 green:.3490 blue:.5961 alpha:1.0];


    }
}

@end

and is use it like this:

并像这样使用它:

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

    static NSString *CellIdentifier = @"Cell";


    MyCustomCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];

    if (cell == nil)
    {


        cell = [[MyCustomCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        //cell.accessoryType = UITableViewCellAccessoryCheckmark;



    }

    CGFloat screenHeight = [UIScreen mainScreen].bounds.size.height;

    if ([UIScreen mainScreen].scale == 2.f && screenHeight == 568.0f)
    {
    UIButton *btn = [UIButton buttonWithType:UIButtonTypeRoundedRect];
    [btn setBackgroundImage:[UIImage imageNamed:@"select2x.png"] forState:UIControlStateNormal];
    [btn addTarget:self action:@selector(SelectButtonTapped:) forControlEvents:UIControlEventTouchUpInside];
    btn.frame = CGRectMake(110, 25, 63, 32);
        //btn.tag = indexPath.row;

        viewProfile = [UIButton buttonWithType:UIButtonTypeRoundedRect];
        viewProfile.frame = CGRectMake(190, 25, 93, 32);
        //[viewProfile setTitle:@"View Profile" forState:UIControlStateNormal];
        [viewProfile setBackgroundImage:[UIImage imageNamed:@"[email protected]"] forState:UIControlStateNormal];
        [viewProfile addTarget:self action:@selector(viewProfileTapped:) forControlEvents:UIControlEventTouchUpInside];
        [cell.contentView addSubview:viewProfile];

    [cell.contentView addSubview:btn];

    }

    else
    {

        UIButton *btn = [UIButton buttonWithType:UIButtonTypeRoundedRect];
        [btn setBackgroundImage:[UIImage imageNamed:@"select.png"] forState:UIControlStateNormal];
        [btn addTarget:self action:@selector(SelectButtonTapped:) forControlEvents:UIControlEventTouchUpInside];
        btn.frame = CGRectMake(110, 25, 63, 32);
        //btn.tag = indexPath.row;

        viewProfile = [UIButton buttonWithType:UIButtonTypeRoundedRect];
        viewProfile.frame = CGRectMake(190, 25, 93, 32);
        //[viewProfile setTitle:@"View Profile" forState:UIControlStateNormal];
        [viewProfile setBackgroundImage:[UIImage imageNamed:@"viewProfile.png"] forState:UIControlStateNormal];
        [viewProfile addTarget:self action:@selector(viewProfileTapped:) forControlEvents:UIControlEventTouchUpInside];
        [cell.contentView addSubview:viewProfile];

        [cell.contentView addSubview:btn];


       }

    return cell;

}

Now each cell contains two buttons which perform an action(I do not make action on cell tap but on button tap) as created like above

现在每个单元格都包含两个按钮,它们执行一个动作(我不在单元格点击上做动作,而是在按钮点击时做动作),就像上面创建的一样

Now what i want is when a user taps a button(say select button) in a particular cell, that particular cell only should become highlighted or shaded or coloured and when user taps a button in another cell, that cell should become highlighted making the previous cell unhighlighted. I have tried storing previous and next indexpath's in 2 different variables but not able to work on them to achieve what i want.

现在我想要的是当用户点击特定单元格中的按钮(比如选择按钮)时,该特定单元格只应突出显示或着色或着色,当用户点击另一个单元格中的按钮时,该单元格应突出显示,使上一个单元格未突出显示。我尝试将上一个和下一个索引路径存储在 2 个不同的变量中,但无法处理它们以实现我想要的。

- (void)SelectButtonTapped:(UIButton *)button
{


    UITableViewCell *cell = (UITableViewCell *)button.superview.superview;

    NSIndexPath *indexPath = [homeTable indexPathForCell:cell];

    // UITableViewCell *cell1 = [homeTable cellForRowAtIndexPath:indexPath];

    //currentSelection = indexPath.row;

    //MyCustomCell *cell1 = (MyCustomCell *)[homeTable cellForRowAtIndexPath:indexPath];

    [[homeTable delegate]tableView:homeTable didSelectRowAtIndexPath:indexPath];

     //MyCustomCell *cell1 = (MyCustomCell *)[homeTable cellForRowAtIndexPath:indexPath];

    /*if(currentSelection == 0)
    {
         [cell1 setSelected:YES animated:YES];
        currentSelection++;
    }
    else
    {
         [cell1 setSelected:YES  animated:YES];

    }*/
   // NSIndexPath *indexPath1 = [homeTable indexPathForCell:cell];

    NSLog(@"current===>%d",currentSelection);



    //[cell1 setSelected:NO animated:YES];

    //if(currentSelection == indexPath)




    //UIButton *clickButton = (UIButton *)sender;
    // NSIndexPath *indexPathHighlight = [NSIndexPath indexPathForRow:button.tag inSection:0];

    //button.tag = indexPathHighlight.row;





    //UITableViewCell *newCell = [homeTable cellForRowAtIndexPath:indexPathHighlight];


    //if(newCell.selected == NO)
    //{
    //[newCell setSelected:YES animated:YES];
     //   [newCell setBackgroundColor:[UIColor lightGrayColor]];
    //}
    //else
   // {
        //[newCell setSelected:NO animated:YES];
        //[newCell setBackgroundColor:[UIColor colorWithRed:232.0f/255.0f green:237.0f/255.0f blue:240.0f/255.0f alpha:1.0f]];
    //}
}

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

    MyCustomCell *cell1 = (MyCustomCell *)[homeTable cellForRowAtIndexPath:indexPath];

    cell1.selectionStyle= UITableViewCellSelectionStyleBlue;

}

You can look at my messy button action method. I have tried many options but with no success. Please help on this.

你可以看看我凌乱的按钮动作方法。我尝试了很多选择,但都没有成功。请帮忙解决这个问题。

回答by Anusha Kottiyal

Uncomment the line :

取消注释该行:

btn.tag = indexPath.row+1000;

Also change the cell selection style as

还将单元格选择样式更改为

[cell setSelectionStyle:UITableViewCellSelectionStyleBlue];

when create it.

创建时。

In SelectButtonTapped method :

在 SelectButtonTapped 方法中:

- (void)SelectButtonTapped:(UIButton *)button
{
  int buttonIndex = button.tag;

  for (int row = 0; row<numberOfRows; row++)
  {
    NSIndexPath indexPathHighlight = [NSIndexPath indexPathForRow:row inSection:0];

    UITableViewCell *newCell = [yourTable cellForRowAtIndexPath:indexPathHighlight];

    if (row == buttonIndex - 1000)
    {
        [newCell setSelected:YES animated:YES];
    }
    else
    {
        [newCell setSelected:NO animated:YES];
    }
  }
}

回答by jjpp

Lets think that your table has only one section and tag of your button be the row number of the corresponding cell.

让我们认为您的表格只有一个部分,您的按钮的标签是相应单元格的行号。

In the button action

在按钮动作中

int rownumber = button.tag;
//print the rownumber and see if you are getting the correct value
UITableViewCell *cell = [tablename cellForRowAtIndexPath:[NSIndexPath     
indexPathForRow:rownumber inSection:0]];

[cell setHighlighted:YES];

回答by rishi

Try this -

尝试这个 -

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    MyCustomCell *cell1 = (MyCustomCell *)[homeTable cellForRowAtIndexPath:indexPath];
    cell1.selectionStyle= UITableViewCellSelectionStyleBlue;
}