首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >UITableView -cell按钮未显示

UITableView -cell按钮未显示
EN

Stack Overflow用户
提问于 2013-07-31 17:44:16
回答 2查看 1.2K关注 0票数 1

当我的滚动视图变得更宽时,我的单元格按钮一度显示出来。我把它变小了,我的纽扣也不见了。我摆弄了一下这个框架,但它似乎不起作用。有什么建议吗?

代码语言:javascript
运行
复制
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
    cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier]autorelease];

        UIButton*button= [UIButton buttonWithType:UIButtonTypeCustom];
        button.frame = CGRectMake(10.0, 0.0, 20,20);
        [button setTitle:@"Tap" forState:UIControlStateNormal];
    button.backgroundColor= [UIColor blackColor]; 
        [button addTarget:self action:@selector(buttonTapped:) forControlEvents:UIControlEventTouchUpInside];
        cell.accessoryView = button;
       [cell.contentView addSubview:button];

}

NSString *cellValue = [selection objectAtIndex:indexPath.row];
cell.textLabel.text = cellValue;

return cell;
EN

回答 2

Stack Overflow用户

发布于 2013-07-31 20:08:59

你必须像你写的那样在if条件之外实现自定义按钮。您必须编写- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath方法,如下所示:

代码语言:javascript
运行
复制
 - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString * CellIdentifier = @"Cell";

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier: CellIdentifier];

if (cell == nil) {
    cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier: CellIdentifier];
}

//custom button outside if condition

UIButton*button= [UIButton buttonWithType:UIButtonTypeCustom];
button.frame = CGRectMake(10.0, 0.0, 20,20);
[button setTitle:@"Tap" forState:UIControlStateNormal];
button.backgroundColor= [UIColor blackColor];
[button addTarget:self action:@selector(buttonTapped:) forControlEvents:UIControlEventTouchUpInside];
cell.accessoryView = button;
[cell.contentView addSubview:button];

return cell;  

}

票数 0
EN

Stack Overflow用户

发布于 2013-07-31 21:50:17

暂时去掉这句话:

代码语言:javascript
运行
复制
cell.accessoryView = button;
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/17966950

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档