【问题标题】:Issues with UITablewView and IBAction senderUITablewView 和 IBAction 发件人的问题
【发布时间】:2013-06-13 19:33:25
【问题描述】:

我有两个完全相同的问题 1)我在一个调用 IBAction 的单元格中有一个 UIButton :

    - (IBAction)deleteFriend:(id)sender
{


CGPoint buttonPosition = [sender convertPoint:CGPointZero toView:self->table];

NSIndexPath *indexPath = [self->table indexPathForRowAtPoint:buttonPosition];
if (indexPath != nil)
   {
    UITableViewCell *cell = [self->table cellForRowAtIndexPath:indexPath];

    [UIView animateWithDuration:0.3 animations:^{

        cell.alpha = 0;
    } completion: ^(BOOL finished) {
        cell.hidden = YES;
        button.hidden = YES;


    }];
}

所以基本上我想在 IBAction 之前确认警报,但这是问题所在。我不知道如何将发件人传递给 UIAlertView 的 buttonIndex 以便它可以完成这个 IBAction 的工作

2) 在代码中,我告诉单元格(IBAction 中的单元格)隐藏,还有我在 cellforRowatIndexPath 中定义的按钮:

button = [UIButton buttonWithType:UIButtonTypeCustom];
[button setBackgroundImage:buttonImage forState:UIControlStateNormal];
button.frame = CGRectMake(272.0f, 15.0f, 30.0f, 30.0f);
[cell button];
[button addTarget:self action:@selector(deleteFriend:)
              forControlEvents:UIControlEventTouchUpInside];

但不知何故,它会随机隐藏其他单元格中的其他按钮。为什么它不只是隐藏在一个单元格中?

提前致谢!

【问题讨论】:

  • 我想帮助你,但我无法理解你的问题的描述。是这两个问题吗?第二期的单元格是什么?尽量说得更清楚。
  • 有两个问题:第一个是发件人定义的单元格,而在后者中,我只是在 cellForRowAtIndexPath 中定义了按钮,但我在上面的 IBAction 中隐藏了这个按钮
  • 您是否有理由不能以编程方式使用单元格的索引并仅操作该单元格中的按钮?
  • 因为我通过单击按钮启动操作,该按钮启动 ibaction,我没有在方法中声明单元格
  • 您的问题很难理解。你想在按下按钮时弹出一个 alertView 吗?

标签: ios uitableview ibaction sender


【解决方案1】:

这里是做什么:创建一个 UITableViewCell 的子类,然后创建一个单元格的 nib 文件,在 tableviews 类中你必须将它添加到 viewDidLoad 方法中

UINib *nib = [UINib nibWithNibName:@"TableViewCell" bundle:nil]; 
[[self tableView] registerNib:nib forCellReuseIdentifier:@"CustomCell"]; 

创建标签、图片、按钮等,将操作方法​​放入单元格类中,让它做你想做的任何事情

在您的 tableviews 类中,这是要使用的代码

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


TableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"CustomCell"];
if (cell == nil) {
    cell = [[TableViewCell alloc] init]; // or your custom initialization
}


cell.name.text=[myName];
return cell;
}

【讨论】:

  • 我不使用附件,它只是 ContentView 中的一个按钮
  • 这个方法用于附件按钮,他使用的是UIButton,而不是附件。
  • 好的,那么如何为单元格创建一个单独的类,并在其中使用您想要的任何操作方法@RaphMaz
  • 这里是做什么:创建一个UITableViewCell的子类,然后创建一个单元格的nib文件,在tableviews类中你必须添加UINib *nib = [UINib nibWithNibName:@"TableViewCell" bundle :零]; [[self tableView] registerNib:nib forCellReuseIdentifier:@"CustomCell"];创建标签、图片、按钮等,将操作方法​​放入单元格类中,让它做你想做的任何事情
  • 我猜这看起来不错。我什至不确定如何使用它,但我会尝试一下,谢谢!
猜你喜欢
  • 2023-03-14
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-01-14
  • 2022-09-27
  • 1970-01-01
相关资源
最近更新 更多