gpt4 book ai didi

ios - 按钮在 TableViewCell 中显示不正确

转载 作者:行者123 更新时间:2023-11-29 03:03:51 25 4
gpt4 key购买 nike

我在 Tableview 的所有单元格中显示按钮时遇到问题。

这是我的代码:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"NotificationCell";
NotificationCell *cell = (NotificationCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];

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

NotificationObject *notification = nil;
notification = [_notificationArray objectAtIndex:indexPath.row];



cell.profileImage.image = notification.profileImage;
cell.profileImage.layer.cornerRadius = cell.profileImage.frame.size.height /2;
cell.profileImage.layer.masksToBounds = YES;
cell.profileImage.layer.borderWidth = 0;
cell.detailTextView.text = notification.action;

UIButton *denyButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
UIButton *acceptButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];

//set the position of the button
denyButton.frame = CGRectMake(cell.frame.origin.x + 285, cell.frame.origin.y + 20, 23, 23);
[denyButton setBackgroundImage:[UIImage imageNamed:@"DenyRequest.png"] forState:UIControlStateNormal];
[denyButton addTarget:self action:@selector(denyButtonPressed:) forControlEvents:UIControlEventTouchUpInside];
denyButton.backgroundColor= [UIColor clearColor];
denyButton.tag = 1000 + indexPath.row;
[cell.contentView addSubview:denyButton];

acceptButton.frame = CGRectMake(cell.frame.origin.x + 240, cell.frame.origin.y + 20, 23, 23);
[acceptButton setBackgroundImage:[UIImage imageNamed:@"AcceptRequest.png"] forState:UIControlStateNormal];
[acceptButton addTarget:self action:@selector(AcceptButtonPressed:) forControlEvents:UIControlEventTouchUpInside];
acceptButton.backgroundColor= [UIColor clearColor];
acceptButton.tag = 1000 + indexPath.row;
[cell.contentView addSubview:acceptButton];


return cell;
}

我遇到的问题是按钮仅显示在顶部单元格中。

如果这是一个简单的错误,我们深表歉意,对于 Objective c 来说仍然相对较新!

最佳答案

您将按钮添加到单元格的内容 View 中,因此按钮的框架不应考虑单元格的框架(您传递的值是相对于单元格的框架,而不是绝对值)。所以,框架设置应该是这样的,

denyButton.frame = CGRectMake(285, 20, 23, 23);
acceptButton.frame = CGRectMake(240, 20, 23, 23);

你还有另一个问题。当您滚动并重复使用单元格时,您将向已有按钮的单元格添加按钮。因此,您要么需要放入一个 if-else 子句来检查单元格中是否已经有按钮,要么在添加新按钮之前删除按钮(就个人而言,我只会在 Storyboard 中制作单元格,而不是必须首先使用所有这些代码)。

关于ios - 按钮在 TableViewCell 中显示不正确,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22999527/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com