2014-11-21 72 views
1

我试图在表格视图单元格中显示带有接受按钮并拒绝按钮(作为子视图)的自定义视图。我执行以下代码:iOS - 在UITableViewCell中未显示自定义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; 
} 

我试图调试它,但方法调用得当。

+0

显示整个tableView:cellForRowAtIndexPath:方法 – TooManyEduardos 2014-11-21 20:43:44

+0

你试图通过你的代码的样子来达到什么样子,它看起来就像你想显示UIView包含单元accessoryView上的两个按钮,纠正我,如果我错了? – ldindu 2014-11-24 17:23:01

+0

是的,这正是我想要实现的 – flizana 2014-11-26 02:28:40

回答

0

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

- (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矩形),CGRectGetMidX(CGRect矩形)和CGRectGetHeight(CGRect矩形)来获得更精确的值来设置每个按钮的框架。这是对UIView框架如何工作的一种误解,我推荐始终使用这些函数来获取子视图的原点和大小,而不是使用文字值。

-1

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

return view; 

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

+0

我在编辑原始问题时询问我忘了复制方法的最后一行。 – flizana 2014-11-23 11:25:19