UITableView -cell按钮未显示

时间:2013-07-31 09:44:16

标签: objective-c uitableview

当我的滚动视图更宽时,我的单元格按钮一度显示出来。我把它缩小了,我的按钮也消失了。我玩过框架,但似乎不起作用。有什么建议?

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

2 个答案:

答案 0 :(得分:0)

如果你写的条件,你必须在外面实现自定义按钮。你必须写 - (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;  

}

答案 1 :(得分:0)

暂时摆脱这句话:

cell.accessoryView = button;
相关问题