gpt4 book ai didi

iOS - 自定义 AccessoryView 未显示在 UITableViewCell 中

转载 作者:行者123 更新时间:2023-11-28 17:52:48 26 4
gpt4 key购买 nike

我试图在表格 View 单元格中显示带有接受按钮和拒绝按钮(作为 subview )的自定义 View 。我实现了以下代码:

tableView: cellForRowAtIndexPath: method

...
if ([status isEqualToString:@"pending"] || [status isEqualToString:@"declined"]){
cell.accessoryView = [self setAccessoryViewForCell:cell];
} else {
cell.accessoryType = UITableViewCellAccessoryNone;
}
...

- (UIView *)setAccessoryViewForCell:(UITableViewCell *)cell
{
UIView *view = [[UIView alloc] initWithFrame:CGRectMake(192, 0, 128, 44)];
UIButton *acceptButton = [[UIButton alloc] initWithFrame:CGRectMake(2, 5, 60, 34)];
UIButton *declineButton = [[UIButton alloc] initWithFrame:CGRectMake(66, 5, 60, 34)];
[acceptButton setTitle:@"A" forState:UIControlStateNormal];
[declineButton setTitle:@"D" forState:UIControlStateNormal];
[acceptButton addTarget:self action:@selector(acceptButtonTapped:) forControlEvents:UIControlEventTouchUpInside];
[declineButton addTarget:self action:@selector(declineButtonTapped:) forControlEvents:UIControlEventTouchUpInside];
[view addSubview:acceptButton];
[view addSubview:declineButton];
return view;
}

我已尝试调试它,但方法调用正确。

最佳答案

最后,问题不在cellForRowAtIndexPath:方法,而在setAccessoryViewForCell:方法。在创建包含两个按钮作为 subview 的 View 时,我真的不应该为框架使用文字值。我没有为 accessoryView 属性设置 View ,而是重写了整个 cellForRowAtIndexPath: 方法并向单元格的 contentView 添加了一个 subview 。

 - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *identifier = @"notificationsCell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
if (!cell){
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:identifier];
}
PFObject *user = [self.objects objectAtIndex:indexPath.row];
NSString *firstName = [user objectForKey:@"firstName"];
NSString *lastName = [user objectForKey:@"lastName"];
cell.textLabel.text = [NSString stringWithFormat:@"%@ %@", firstName, lastName];

UIView *view = [UIView new];
view.frame = CGRectMake(230, 2, 80, 40);
view.backgroundColor = [UIColor whiteColor];

UIButton *acceptButton = [UIButton buttonwithType:UIButtonTypeCustom];
UIButton *declineButton = [UIButton buttonWithType:UIButtonTypeCustom];
[acceptButton setTitle:@"" forState:UIControlStateNormal];
[declineButton setTitle:@"" forState:UIControlStateNormal];
[acceptButton setImage:[UIImage imageNamed:@"Ok.png"] forState:UIControlStateNormal];
[declineButton setImage:[UIImage imageNamed:@"Close.png"] forState:UIControlStateNormal];
[acceptButton addTarget:self action:@selector(acceptButtonTapped:) forControlEvents:UIControlEventTouchUpInside];
[declineButton addTarget:self action:@selector(declineButtonTapped:) forControlEvents:UIControlEventTouchUpInside];
acceptButton.frame = CGRectMake(CGRectGetMinX(view.bounds), CGRectGetMinY(view.bounds), CGRectGetWidth(view.bounds)/2, CGRectGetHeight(view.bounds));
declineButton.frame = CGRectMake(CGRectGetMidX(view.bounds), CGRectGetMinY(view.bounds), CGRectGetWidth(view.bounds)/2, CGRectGetHeight(view.bounds));

[view addSubview:acceptButton];
[view addSubview:declineButton];
[cell.contentView addSubview:view];

return cell;
}

主要区别在于,在为每个按钮设置框架时,我使用了文字值(不是一个好的做法),现在我使用了 CGRect 函数 CGRectGetMinX(CGRect rect)、CGRectGetMinY(CGRect rect)、CGRectGetWidth(CGRect rect) , CGRectGetMidX(CGRect rect) 和 CGRectGetHeight(CGRect rect) 以获得更准确的值来设置每个按钮的框架。这是对 UIView 框架工作方式的误解,我建议始终使用这些函数来获取 subview 的来源和大小,而不是使用文字值。

关于iOS - 自定义 AccessoryView 未显示在 UITableViewCell 中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27069828/

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