gpt4 book ai didi

iphone - 如何为一个部分中的特定行滑动 tableviewcell

转载 作者:塔克拉玛干 更新时间:2023-11-02 10:10:27 25 4
gpt4 key购买 nike

我有一个自定义的 UITableview,它分为几个部分,我已经在其中实现了 UISwipeGestureRecognizer。当我滑动表格 View 单元格时,会出现一个 UIButton。我现在面临的问题是,当我滑动第一个表格 View 单元格时,连续部分中的另一个单元格也可以识别滑动手势。我无法找到如何在不刷其他部分的其他单元格的情况下刷特定部分的单元格。

我只想滑动,我不想删除/插入或添加复选标记。

更新....这是我的customCell.m Tableview = customTableView

       - (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
if (self)
{
self.backgroundColor = [UIColor clearColor];
self.contentView.backgroundColor = [UIColor clearColor];

self.symptomCellImageView.contentMode=UIViewContentModeScaleToFill;


swipeButton = [[UIButton alloc]init];
swipeButton .frame = CGRectMake(220.0, 8.0, 30.0, 30.0);
swipeButton .hidden = NO;

}

在我的 MainViewController.m 中

  -(void)viewDidLoad
{

symptomSwipeLeft = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(swipeHandleLeft)];
symptomSwipeLeft .numberOfTouchesRequired = 1;
symptomSwipeLeft .direction = UISwipeGestureRecognizerDirectionLeft;
[customTableView addGestureRecognizer:symptomSwipeLeft];
[self addGestureRecognizer:symptomSwipeRight];

symptomSwipeRight = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(swipeHandleRight)];
symptomSwipeRight.numberOfTouchesRequired = 1;
symptomSwipeRight.direction = UISwipeGestureRecognizerDirectionRight;
[customTableView addGestureRecognizer:symptomSwipeRight];
[self addGestureRecognizer:symptomSwipeRight];

}


- (void)swipeHandleLeft:(UISwipeGestureRecognizer *)aSwipeGestureRecognizer
{
CGPoint location = [aSwipeGestureRecognizer locationInView:customTableView ];
NSIndexPath * indexPath = [customTableView indexPathForRowAtPoint:location];

if(indexPath)
{
UITableViewCell * cell = (UITableViewCell *)[customTableView cellForRowAtIndexPath:indexPath];
[cell.swipeButton addTarget:self action:@selector(secondPageButton:) forControlEvents:UIControlEventTouchUpInside];

}
}



- (void)swipeHandleRight:(UISwipeGestureRecognizer *)aSwipeGestureRecognizer
{
CGPoint location = [aSwipeGestureRecognizer locationInView:customTableView ];
NSIndexPath * indexPath = [customTableViewindexPathForRowAtPoint:location];

if(indexPath)
{
UITableViewCell * cell = (UITableViewCell *)[customTableView cellForRowAtIndexPath:indexPath];
[cell.swipeButton addTarget:self action:@selector(secondPageButton:) forControlEvents:UIControlEventTouchUpInside];
}
}

最佳答案

尝试:

- (void)handleSwipe:(UISwipeGestureRecognizer *)aSwipeGestureRecognizer; {
CGPoint location = [aSwipeGestureRecognizer locationInView:_tableView];
NSIndexPath * indexPath = [_tableView indexPathForRowAtPoint:location];

if(indexPath){
UITableViewCell * cell = (UITableViewCell *)[_tableView cellForRowAtIndexPath:indexPath];
[cell whateverMethodYouWant];
}
}

作为旁注,您收到对多个单元格的调用的原因是该行不够唯一。在你们所有的部分中都有一个 row = 0。因此,如果您希望每个单元格的 .tag 属性附加一个唯一的数字,您需要知道任何部分中的最大行数(称之为 largestNumberOfRows ), 然后计算:

cell.swipeButton.tag = (indexPath.section * largestNumberOfRows) + indexPath.row;

希望对您有所帮助!

编辑:

要使用上述方法,请在您的viewDidLoad; 方法中添加以下代码:

UISwipeGestureRecognizer * recognizer = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(handleSwipe:)];
[recognizer setDirection:(UISwipeGestureRecognizerDirectionRight | UISwipeGestureRecognizerDirectionLeft)];
[_tableView addGestureRecognizer:recognizer];

编辑 2

查看您的代码,您将手势识别器放在了错误的文件中。这是设置:

在您的 MainViewController.m 文件中:

- (void)viewDidLoad; {
[super viewDidLoad];
UISwipeGestureRecognizer * recognizer = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(handleSwipe:)];
[recognizer setDirection:(UISwipeGestureRecognizerDirectionRight | UISwipeGestureRecognizerDirectionLeft)];
[_tableView addGestureRecognizer:recognizer];
}

- (void)handleSwipe:(UISwipeGestureRecognizer *)aSwipeGestureRecognizer; {
CGPoint location = [aSwipeGestureRecognizer locationInView:_tableView];
NSIndexPath * indexPath = [_tableView indexPathForRowAtPoint:location];

if(indexPath){
UITableViewCell * cell = (UITableViewCell *)[_tableView cellForRowAtIndexPath:indexPath];

if(aSwipeGestureRecognizer.direction == UISwipeGestureRecognizerDirectionRight){
[cell symptomCellSwipeRight];
}
else if(aSwipeGestureRecognizer.direction == UISwipeGestureRecognizerDirectionLeft){
[cell symptomCellSwipeLeft];
}
}
}

- (UITableViewCell *)cellForRowAtIndexPath:(NSIndexPath *)indexPath; {
// Initial your cell. Then add:
[cell.swipeButton addTarget:self action:@selector(secondPageButton:) forControlEvents:UIControlEventTouchUpInside];
}

在您的 PPsymptomTableCell.m 文件中:

- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier; {
if((self = [super initWithStyle:style reuseIdentifier:reuseIdentifier])){
self.backgroundColor = [UIColor clearColor];
self.contentView.backgroundColor = [UIColor clearColor];

self.symptomCellImageView.contentMode=UIViewContentModeScaleToFill;

// SHARE ON FACEBOOK BUTTON //

shareFacebookButton = [[UIButton alloc]init];
shareFacebookButton.frame = CGRectMake(220.0, 8.0, 30.0, 30.0);
shareFacebookButton.hidden = NO;

// DISPLAY NOTIFICATION IMAGE //

selectedCellImageDisplay = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"selectedSymptomImage.png"]];
selectedCellImageDisplay.frame = CGRectMake(240.0, 8.0, 30.0, 30.0);

// SYMPTOM NAME //

symptomCellLabel=[[UILabel alloc]initWithFrame:CGRectMake(15.0,0.0 ,280.0,40.0)];
symptomCellLabel.font=[UIFont fontWithName:@"Rockwell" size:17];
symptomCellLabel.textColor=[UIColor blackColor];
symptomCellLabel.backgroundColor=[UIColor clearColor];

[self.contentView addSubview:symptomCellLabel];
[self.contentView addSubview:selectedCellImageDisplay];
[self.contentView addSubview:shareFacebookButton];
}
return self;
}

- (void)symptomCellSwipeLeft; {
[UIView beginAnimations:@"HideView" context:nil];
symptomCellLabel.frame = CGRectMake(-183.0, 0.0, 280.0, 40.0);
selectedCellImageDisplay.frame = CGRectMake(-340.0, 8.0, 30.0, 30.0);
shareFacebookButton.frame = CGRectMake(120.0, 8.0, 80.0, 30.0);
shareFacebookButton.hidden = NO;
shareFacebookButton.backgroundColor = [UIColor redColor];

[UIView setAnimationDuration:0.3f];
[UIView commitAnimations];
}


- (void)symptomCellSwipeRight; {
[UIView beginAnimations:@"HideView" context:nil];
symptomCellLabel.frame = CGRectMake(15.0,0.0 ,280.0,40.0);
selectedCellImageDisplay.frame = CGRectMake(240.0, 8.0, 30.0, 30.0);
shareFacebookButton.frame = CGRectMake(220.0, 8.0, 30.0, 30.0);
shareFacebookButton.hidden = YES;

[UIView setAnimationDuration:0.3f];
[UIView commitAnimations];
}

这应该能让一切正常工作。

关于iphone - 如何为一个部分中的特定行滑动 tableviewcell,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16152640/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com