需要对具有自定义 UITableViewCell 的嵌套 TableView 中的 UIButton 按下采取操作

发布于 2025-01-04 15:42:02 字数 4380 浏览 0 评论 0原文

我有一个 UITableViewController (称为详细信息)。在第 1 行的 tableView 中,我有另一个水平旋转的 UITableView。该表视图中的每个单元格都是一个自定义 UITableViewCell(称为 DetailsHorizo​​ntalPhotoCell),它显示带有图像的按钮。当按下照片(按钮)时,我很难采取行动。

如果我在 DetailsHorizo​​ntalPhotoCell.h/.m 文件中有 IBAction 代码,那么调用 IBAction 对我来说不是问题,但是我需要在按下按钮时呈现模态 VC。该代码不属于和/或在 tableViewCell 中工作 - 它需要位于控制器中(详细信息)。话虽这么说,我不知道如何编码。这是我到目前为止所拥有的。

Details.m:

if( (indexPath.row == 1) && ([detailsObject_.photo count] > 0) )
{
    NSLog(@"** In Photo Section **");

    static NSString *CellIdentifier = @"cellPhoto";
    DetailsHorizontalPhotoCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if(cell == nil)
    {
        cell = [[DetailsHorizontalPhotoCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
    }

    CGAffineTransform rotateTable = CGAffineTransformMakeRotation(-M_PI_2);
    cell.horizontalTableView.transform = rotateTable;

    NSArray *photoArray = [[NSArray alloc] initWithArray:detailsObject_.photo];

    cell.horizontalTableView.frame = CGRectMake(0, 0, cell.horizontalTableView.frame.size.width, cell.horizontalTableView.frame.size.height); 
    cell.contentArray = [NSArray arrayWithArray:photoArray];
    cell.horizontalTableView.allowsSelection = YES;

    return cell;
}

DetailsHorizo​​ntalPhotoCell.m:

@synthesize horizontalTableView = horizontalTableView_;
@synthesize contentArray = contentArray_;
@synthesize imageButton = imageButton_;

// *** a bunch of code which is not relevant to this question snipped out here... 


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"Cell";

    UITableViewCell *cell = [self.horizontalTableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if(cell == nil)
    {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier];
        [cell setSelectionStyle:UITableViewCellSelectionStyleNone];
    }

    for(UIButton *button in cell.subviews)
    {
        [button removeFromSuperview];
    }

    // create image and button
    UIImage *image = [UIImage imageNamed:[contentArray_ objectAtIndex:indexPath.row]];
    self.imageButton = [[UIButton alloc] initWithFrame:CGRectMake(5, 5, 240, 240)];

    // setup the button
    [imageButton_ setImage:image forState:UIControlStateNormal];
    imageButton_.layer.masksToBounds = YES;
    imageButton_.layer.cornerRadius = 5.0;
    imageButton_.layer.borderWidth = 1.0;
    imageButton_.layer.borderColor = [[UIColor grayColor] CGColor];

    // rotate the button
    CGAffineTransform rotateButton = CGAffineTransformMakeRotation(M_PI_2);
    imageButton_.transform = rotateButton;

    // this detects the click of each photo and triggers the IBAction
    [imageButton_ addTarget:self action:@selector(photoButton:) forControlEvents:UIControlEventTouchUpInside];

    /// do more stuff yada yada yada... <snipped code> and return cell;
}


// the Action (which I know should NOT be in this UITableViewCell class)
- (IBAction)photoButton:(id)sender 
{
    CGPoint hitPoint = [sender convertPoint:CGPointZero toView:self.horizontalTableView];
    NSIndexPath *hitIndex = [self.horizontalTableView indexPathForRowAtPoint:hitPoint];

    NSLog(@"Image clicked, index: %d", hitIndex.row);

    // presentModalViewController here - but you can't do that from here! Must be in the Details controller
}

所以,知道我无法从单元格执行presentModalViewController,我尝试将该代码移动到Details类中(如下)

将IBAction添加到Details.h和.m

- (IBAction)photoButton:(id)sender 
{
    DetailsHorizontalPhotoCell *cell = [[DetailsHorizontalPhotoCell alloc] init];

    CGPoint hitPoint = [sender convertPoint:CGPointZero toView:cell.horizontalTableView];
    NSIndexPath *hitIndex = [cell.horizontalTableView indexPathForRowAtPoint:hitPoint];

    NSLog(@"Image clicked, index: %d", hitIndex.row);
}

并添加单击事件到详细信息 cellForRowAtIndexPath 方法中的照片按钮。

// Blaaahh! Tried a million combinations of something like below but cannot get it to work... 
//[cell.imageButton addTarget:cell.horizontalTableView action:@selector(photoButton:) forControlEvents:UIControlEventTouchUpInside];

PS - 我正在使用 IOS5、Xcode 4.2 w/ ARC

I have a UITableViewController (called Details). In that tableView on row 1 I have another UITableView which I rotate horizontally. Each cell in that tableview is a custom UITableViewCell (called DetailsHorizontalPhotoCell) which displays a button with an image. I am having a hard time taking action when the photos (the buttons) are pressed.

Calling an IBAction is not a problem for me if I have that IBAction code in the DetailsHorizontalPhotoCell.h/.m files however I need to present a modal VC upon the button presses. That code does not belong and/or work in a tableViewCell - it needs to be in the controller (Details). That being said I can't figure out how to code it. This is what I have so far.

Details.m:

if( (indexPath.row == 1) && ([detailsObject_.photo count] > 0) )
{
    NSLog(@"** In Photo Section **");

    static NSString *CellIdentifier = @"cellPhoto";
    DetailsHorizontalPhotoCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if(cell == nil)
    {
        cell = [[DetailsHorizontalPhotoCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
    }

    CGAffineTransform rotateTable = CGAffineTransformMakeRotation(-M_PI_2);
    cell.horizontalTableView.transform = rotateTable;

    NSArray *photoArray = [[NSArray alloc] initWithArray:detailsObject_.photo];

    cell.horizontalTableView.frame = CGRectMake(0, 0, cell.horizontalTableView.frame.size.width, cell.horizontalTableView.frame.size.height); 
    cell.contentArray = [NSArray arrayWithArray:photoArray];
    cell.horizontalTableView.allowsSelection = YES;

    return cell;
}

DetailsHorizontalPhotoCell.m:

@synthesize horizontalTableView = horizontalTableView_;
@synthesize contentArray = contentArray_;
@synthesize imageButton = imageButton_;

// *** a bunch of code which is not relevant to this question snipped out here... 


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"Cell";

    UITableViewCell *cell = [self.horizontalTableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if(cell == nil)
    {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier];
        [cell setSelectionStyle:UITableViewCellSelectionStyleNone];
    }

    for(UIButton *button in cell.subviews)
    {
        [button removeFromSuperview];
    }

    // create image and button
    UIImage *image = [UIImage imageNamed:[contentArray_ objectAtIndex:indexPath.row]];
    self.imageButton = [[UIButton alloc] initWithFrame:CGRectMake(5, 5, 240, 240)];

    // setup the button
    [imageButton_ setImage:image forState:UIControlStateNormal];
    imageButton_.layer.masksToBounds = YES;
    imageButton_.layer.cornerRadius = 5.0;
    imageButton_.layer.borderWidth = 1.0;
    imageButton_.layer.borderColor = [[UIColor grayColor] CGColor];

    // rotate the button
    CGAffineTransform rotateButton = CGAffineTransformMakeRotation(M_PI_2);
    imageButton_.transform = rotateButton;

    // this detects the click of each photo and triggers the IBAction
    [imageButton_ addTarget:self action:@selector(photoButton:) forControlEvents:UIControlEventTouchUpInside];

    /// do more stuff yada yada yada... <snipped code> and return cell;
}


// the Action (which I know should NOT be in this UITableViewCell class)
- (IBAction)photoButton:(id)sender 
{
    CGPoint hitPoint = [sender convertPoint:CGPointZero toView:self.horizontalTableView];
    NSIndexPath *hitIndex = [self.horizontalTableView indexPathForRowAtPoint:hitPoint];

    NSLog(@"Image clicked, index: %d", hitIndex.row);

    // presentModalViewController here - but you can't do that from here! Must be in the Details controller
}

So, knowing that I can't perform a presentModalViewController from the Cell I try to move that code into the Details class (below)

Add the IBAction to Details.h and .m

- (IBAction)photoButton:(id)sender 
{
    DetailsHorizontalPhotoCell *cell = [[DetailsHorizontalPhotoCell alloc] init];

    CGPoint hitPoint = [sender convertPoint:CGPointZero toView:cell.horizontalTableView];
    NSIndexPath *hitIndex = [cell.horizontalTableView indexPathForRowAtPoint:hitPoint];

    NSLog(@"Image clicked, index: %d", hitIndex.row);
}

And add the click event to the photo button in the Details cellForRowAtIndexPath method.

// Blaaahh! Tried a million combinations of something like below but cannot get it to work... 
//[cell.imageButton addTarget:cell.horizontalTableView action:@selector(photoButton:) forControlEvents:UIControlEventTouchUpInside];

PS - I'm using IOS5, Xcode 4.2 w/ ARC

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

情泪▽动烟 2025-01-11 15:42:02

首先,不需要在每次显示单元格时添加按钮。您只需在创建新单元格时添加单元格的子视图。这样您将获得更好的滚动性能。

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"Cell";

    UITableViewCell *cell = [self.horizontalTableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if(cell == nil)
    {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier];
        [cell setSelectionStyle:UITableViewCellSelectionStyleNone];

        // Add image button to cell
        UIButton *button = [[UIButton alloc] initWithFrame:CGRectMake(5, 5, 240, 240)];
        // set tag so you can get the button later
        button.tag = 1021;
        [button addTarget:self action:@selector(photoButton:) forControlEvents:UIControlEventTouchUpInside]
        // more button config
        [cell.contentView addSubView:button];
    }
    // get image button
    UIButton *button = [cell.contentView viewWithTag:1021];
    // configure image button
    UIImage *image = [UIImage imageNamed:[contentArray_ objectAtIndex:indexPath.row]];
    [button setImage:image forState:UIControlStateNormal];
    return cell;
}

接下来,您可以使用按钮(=发送者)上的超级视图返回到 UITableViewCell。通过单元格,您可以获得该单元格的索引路径。

- (IBAction)photoButton:(id)sender 
{
    UIView *contentView = [sender superview];
    UITableViewCell *cell = [contentView superview];
    NSIndexPath *indexPath = [self.tableView indexPathForCell:cell];
}

First of all there is no need to add the button each time a cell is displayed. You only have to add the subviews of the cell when you create a new cell. You'll get better scrolling performance this way.

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"Cell";

    UITableViewCell *cell = [self.horizontalTableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if(cell == nil)
    {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier];
        [cell setSelectionStyle:UITableViewCellSelectionStyleNone];

        // Add image button to cell
        UIButton *button = [[UIButton alloc] initWithFrame:CGRectMake(5, 5, 240, 240)];
        // set tag so you can get the button later
        button.tag = 1021;
        [button addTarget:self action:@selector(photoButton:) forControlEvents:UIControlEventTouchUpInside]
        // more button config
        [cell.contentView addSubView:button];
    }
    // get image button
    UIButton *button = [cell.contentView viewWithTag:1021];
    // configure image button
    UIImage *image = [UIImage imageNamed:[contentArray_ objectAtIndex:indexPath.row]];
    [button setImage:image forState:UIControlStateNormal];
    return cell;
}

Next you can use use superview on the button (= sender) to go back to the UITableViewCell. With the cell you can get the indexPath of that cell.

- (IBAction)photoButton:(id)sender 
{
    UIView *contentView = [sender superview];
    UITableViewCell *cell = [contentView superview];
    NSIndexPath *indexPath = [self.tableView indexPathForCell:cell];
}
如果没有你 2025-01-11 15:42:02

您是否在检测按钮按下时遇到问题,或者在让 presentModalViewController 在表格视图中使用时遇到问题?如果您在检测按钮按下时遇到问题,那么您可能只需要:

[button setExclusiveTouch:YES];

如果您遇到问题是因为无法在 tableView 中使用 presentModalViewController ,那么您可能可以使用:

[[NSNotificationCenter defaultCenter] addObserver: selector: name: object:];

通知您的“Details 类”使用presentModalViewController

Are you having trouble detecting the button press or are you having trouble getting presentModalViewController to work from with in the tableview? If you having trouble detecting the button press then you might simply need:

[button setExclusiveTouch:YES];

If your having trouble because you cannot use presentModalViewController from within the tableView then yo might be able to use:

[[NSNotificationCenter defaultCenter] addObserver: selector: name: object:];

To notify your "Details class" to use presentModalViewController

人生戏 2025-01-11 15:42:02

您可以添加特定标签或使用 [[button superview] superview] 访问 uitableview

you can add a specific TAG or access to uitableview with [[button superview] superview]

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文