Objective c UITableView-单元格按钮未显示

Objective c UITableView-单元格按钮未显示,objective-c,uitableview,Objective C,Uitableview,有一次,当我的滚动视图更宽时,我的手机按钮出现了。我把它变小了,我的钮扣也不见了。我已经摆弄过这个框架,但它似乎不起作用。有什么建议吗 - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { static NSString *CellIdentifier = @"Cell"; UITableViewCell *cell = [tableVie

有一次,当我的滚动视图更宽时,我的手机按钮出现了。我把它变小了,我的钮扣也不见了。我已经摆弄过这个框架,但它似乎不起作用。有什么建议吗

- (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;

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

 - (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;  

}

暂时抛开这句话:

cell.accessoryView = button;

是否调用过cell==nil分支?或者问另一个问题:您是否为重用IDEnitifier单元注册了UITableViewCell类?或者你在用故事板?不是用笔尖。不确定您所说的注册问题是什么意思,但if语句被调用。您为什么将此按钮指定给附件视图?据我所知,这将使表使用按钮,尽管它已经是一个子视图,并将其作为子视图添加到附件视图应位于的框架中。这很可能就在可见的框架之外。通过这样做,作为子视图的现有关联将被删除,因为每个视图在子视图层次结构中一次只能有一次。它也可能改变框架。在赋值和addSubview语句之后,您是否尝试过调试或登录按钮的框架?为什么会这样?这样,每次重用表单元格时,都会创建并添加UIButton的新实例。当用户向下和向上滚动一点时,每个单元格中都会有数百个按钮,当然都是同一个框架。感谢您的帮助,但结果是我忽略了我的表有多大。仔细想想-我没有尝试过-在设置accessoryView属性后设置框架可能会有所帮助。