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

时间:2014-11-21 20:31:22

标签: ios objective-c uitableview accessoryview

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

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

我试过调试它,但是方法被适当调用。

2 个答案:

答案 0 :(得分:0)

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

 - (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框架如何工作的误解,我建议总是使用这些函数来获取子视图的原点和大小,而不是使用文字值。

答案 1 :(得分:-1)

问题似乎是你没有从 setAccessoryViewForCell 方法返回UIView。

return view;

请从上述方法返回视图,它可能会解决您的问题。