1

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

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

2 回答 2

0

暂时摆脱这种说法:

cell.accessoryView = button;
于 2013-07-31T13:50:16.593 回答
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;  

}

于 2013-07-31T12:08:59.020 回答