将按钮添加到UITableViewCell的附件视图

编程入门 行业动态 更新时间:2024-10-10 13:24:29
本文介绍了将按钮添加到UITableViewCell的附件视图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

目标:当用户选择一个单元格时,会向该单元格添加一个按钮.在我的didSelectRowAtIndexPath函数中,我具有以下内容:

Goal: when a user selects a cell, a button is added to that cell. Within my didSelectRowAtIndexPath function I have the following:

UIButton *downloadButton = [[UIButton alloc] init]; downloadButton.titleLabel.text = @"Download"; [downloadButton setFrame:CGRectMake(40, 0, 100, 20)]; [[self.tableView cellForRowAtIndexPath:indexPath].accessoryView addSubview:downloadButton]; [[self.tableView cellForRowAtIndexPath:indexPath].accessoryView setNeedsLayout]; [downloadButton release];

不幸的是,这似乎没有任何作用.我要重新绘制单元格校正吗?我需要以其他方式添加它吗?

Unfortunately that doesn't seem to do anything. Am I redrawing the cell correction? Do I need to add it another way?

推荐答案

尝试以下代码块,而不是上面提供的代码块:

Try this block of code instead of the block you provided above:

UIButton *downloadButton = [UIButton buttonWithType:UIButtonTypeRoundedRect]; [downloadButton setTitle:@"Download" forState:UIControlStateNormal]; [downloadButton setFrame:CGRectMake(0, 0, 100, 35)]; [tableView cellForRowAtIndexPath:indexPath].accessoryView = downloadButton;

这应该显示按钮,但是您仍然需要使用addTarget将某种选择器连接到该按钮. (我不确定在这种情况下,是否侦听annexingButtonTappedForRowWithIndexPath委托是否可以正常工作,请先尝试该操作,看看它是否在您按下按钮时触发.)

This should display the button, but you will still need to hook up some kind of selector to it using addTarget. (I am not sure if listening in for the accessoryButtonTappedForRowWithIndexPath delegate will work in this case, try that first and see if it fires on your button press.)

更多推荐

将按钮添加到UITableViewCell的附件视图

本文发布于:2023-11-09 04:06:55,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1571358.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:视图   按钮   附件   UITableViewCell

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!