IOS自动调整表格单元格大小w。展开弹出菜单(如tweetbot)

IOS自动调整表格单元格大小w。展开弹出菜单(如tweetbot),ios,uitableview,accordion,expand,autoresize,Ios,Uitableview,Accordion,Expand,Autoresize,我知道这可能会是一件让人头疼的事,但这需要去做。我最终得到了调整单元格大小和标签大小的tableview,这样每个单元格中的所有文本都能正确显示 注意:comment&bump按钮不是我希望在单元格展开时显示的弹出菜单,这两个按钮应始终显示,我希望添加的菜单将是一组4个按钮,当单元格展开时将显示在这些按钮下 但现在我想在每个单元格中添加一个弹出菜单,如tweetbot,如下所示 但事实证明,这是相当困难的,因为我正在使用自动布局约束调整标签的大小。我遇到了这个例子,说明了如何在github上实现

我知道这可能会是一件让人头疼的事,但这需要去做。我最终得到了调整单元格大小和标签大小的tableview,这样每个单元格中的所有文本都能正确显示


注意:comment&bump按钮不是我希望在单元格展开时显示的弹出菜单,这两个按钮应始终显示,我希望添加的菜单将是一组4个按钮,当单元格展开时将显示在这些按钮下

但现在我想在每个单元格中添加一个弹出菜单,如tweetbot,如下所示

但事实证明,这是相当困难的,因为我正在使用自动布局约束调整标签的大小。我遇到了这个例子,说明了如何在github上实现类似于Tweetbot的东西,它看起来非常简单实用

问题是,本例假设所有单元格大小相同,并且它们的大小不对单个单元格进行动态调整

PublicFeedViewController.M

自定义FeedItemCell.M


“凹凸”按钮是否应该展开单元格?单元格不应修改自己的框架。该按钮应使用委托模式通知视图控制器您的按钮想要变大。然后,视图控制器应返回与heightForRowAtIndexPath不同的值:对于该单元格,并调用[self.FeedTable BeginUpdate];[self.FeedTable endUpdates];。谢谢你的旁白,但这对我试图解决的问题找到一个实际的解决方案毫无帮助。问题并不是我无法调整单元格的大小,而是我无法在保留自动布局约束的同时为单元格提供一个弹出菜单,以确保标签框足够大,可以显示所有文本。如果我像现在这样在我的单元格上实现github的解决方案,那么当您展开单元格时,它也会将标签拉伸到超出需要的程度,导致标签与注释和凹凸按钮重叠注意:注释和凹凸按钮不是我希望在单元格展开时显示的弹出菜单,这两个按钮应始终显示,我要添加的菜单将是一组4个按钮,当单元格展开时,这些按钮将显示在这些按钮下。您能否显示用于标签的自动布局约束?nvm。。。刚意识到它的威力,现在就要更新了
#import "PublicFeedViewController.h"
#import "FeedItemCell.h"
#import "AFNetworking.h"
#import "UIImageView+WebCache.h"
#import "InboxDetailViewController.h"
#import "SWRevealViewController.h"
#import "CommentsViewController.h"
#import "NSDate+TimeAgo.h"


@interface PublicFeedViewController (){
    NSArray *NameLabel;
    NSArray *StatusLabel;
    NSMutableArray *feedArray;
}

@end

@implementation PublicFeedViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];

    //The below code prompts the user for push notifications. If allowed, code in AppDelegate takes over and stores the token.
    [[UIApplication sharedApplication] registerForRemoteNotificationTypes:(UIRemoteNotificationTypeBadge | UIRemoteNotificationTypeSound | UIRemoteNotificationTypeAlert)];
 // Do any additional setup after loading the view.
    self.FeedTable.dataSource=self;
    self.FeedTable.delegate=self;

    // Set the side bar button action. When it's tapped, it'll show up the sidebar.
    _sidebarButton.target = self.revealViewController;
    _sidebarButton.action = @selector(revealToggle:);

    // Set the gesture
    [self.view addGestureRecognizer:self.revealViewController.panGestureRecognizer];

    AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
    NSDictionary *parameters = @{@"foo": @"bar"};
    [UIApplication sharedApplication].networkActivityIndicatorVisible = TRUE;
    [manager POST:@"http://" parameters:parameters success:^(AFHTTPRequestOperation *operation, id responseObject) {
        //NSLog(@"JSON: %@", responseObject);

        self->feedArray = [responseObject objectForKey:@"feed"];

        [self.FeedTable reloadData];
        [UIApplication sharedApplication].networkActivityIndicatorVisible = FALSE;
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        NSLog(@"Error: %@", error);
    }];


}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 1;
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return feedArray.count;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{


    NSString *CellIdentifier=@"Cell";

    FeedItemCell *Cell=[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if(!Cell){
        Cell = [[FeedItemCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];

    }


    NSLog(@"FEED ARRAY: %@", self->feedArray);
    NSDictionary *tempDictionary= [self->feedArray objectAtIndex:indexPath.row];
    // Display recipe in the table cell
    NSString *thumb_img = [tempDictionary objectForKey:@"thumb_img"];
    NSString *thumb_path=@"http://";
    NSString *thumb_url = [thumb_path stringByAppendingString:thumb_img];



    Cell.NameLabel.text=[tempDictionary objectForKey:@"first_name"];




    Cell.StatusLabel.text=[tempDictionary objectForKey:@"message"];
    Cell.msg_id=[tempDictionary objectForKey:@"msg_id"];
    //Cell.status=[tempDictionary objectForKey:@"message"];
    Cell.StatusLabel.lineBreakMode=0;
    Cell.StatusLabel.numberOfLines=0;
    NSString *commentCount = [tempDictionary objectForKey:@"comment_count"];
    NSString *commentButtonText =[NSString stringWithFormat:@"Comments ( %@ )",commentCount];
    [Cell.commentButton setTitle:commentButtonText  forState: UIControlStateNormal];
    NSString *bumpCount = [tempDictionary objectForKey:@"bump_count"];
    NSString *bumpButtonText =[NSString stringWithFormat:@"Bumps ( %@ )",bumpCount];
    [Cell.bumpButton setTitle:bumpButtonText  forState: UIControlStateNormal];
    //[Cell.StatusLabel sizeToFit];
    NSString *created_string=[tempDictionary objectForKey:@"created"];
    double created_double = created_string.doubleValue;
    NSDate *date = [[NSDate alloc] initWithTimeIntervalSince1970:created_double];
    NSString *ago = [date timeAgo];
    Cell.timeLabel.text=ago;




    //Cell.DefaultImg.image = [UIImage imageNamed:@"buhz_mini_logo.png"];

    [Cell.DefaultImg setImageWithURL:[NSURL URLWithString:thumb_url]
                    placeholderImage:[UIImage imageNamed:@".png"]];
    return Cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    //Ideally you should do lazy loading so that instead of creating a new textView each time, you just reuse the same one.
    UITextView *temp = [[UITextView alloc] initWithFrame:CGRectMake(82, 26, self.FeedTable.frame.size.width, 18)]; //This initial size doesn't matter
    NSDictionary *tempDictionary= [self->feedArray objectAtIndex:indexPath.row];
    NSString *status = [tempDictionary objectForKey:@"message"];
    temp.font =[UIFont fontWithName:@"System" size:12];
    temp.text = status;
    [temp isHidden];

    CGFloat textViewWidth = 218;
    CGRect tempFrame = CGRectMake(82,26,textViewWidth,18); //The height of this frame doesn't matter.
    CGSize tvsize = [temp sizeThatFits:CGSizeMake(tempFrame.size.width, tempFrame.size.height)]; //This calculates the necessary size so that all the text fits in the necessary width.

    //Add the height of the other UI elements inside your cell

    return tvsize.height + 70;


}

@end
#import "FeedItemCell.h"
#import "WYPopoverController/WYPopoverController.h"
#import "WYPopoverController/WYStoryboardPopoverSegue.h"
#import "CommentsViewController.h"
#import "NSDate+TimeAgo.h"


@interface FeedItemCell() <WYPopoverControllerDelegate>
{

    WYPopoverController* commentsPopoverController;
}
- (IBAction)open:(id)sender;
- (void)close:(id)sender;
@end


@implementation FeedItemCell
@synthesize commentButton;
- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        // Initialization code
    }
    return self;
}




-(IBAction)bump:(id)sender{

    [self expand];

}

- (IBAction)open:(id)sender
{
    [self showpopover:sender];
}

- (void)close:(id)sender
{
    [commentsPopoverController dismissPopoverAnimated:YES];
    commentsPopoverController.delegate = nil;
    commentsPopoverController = nil;
}

-(void)expand
{
    CGRect  oldFrame = [self frame];

    [self setFrame:CGRectMake(  oldFrame.origin.x,
                              oldFrame.origin.y,
                              oldFrame.size.width,
                              oldFrame.size.height * 2)];
}




-(void)contract
{
    CGRect  oldFrame = [self frame];

    [self setFrame:CGRectMake(  oldFrame.origin.x,
                              oldFrame.origin.y,
                              oldFrame.size.width,
                              oldFrame.size.height / 2)];
}


- (IBAction)showpopover:(id)sender
{
    if (commentsPopoverController == nil)
    {
        UIView *btn = (UIView*)sender;
         UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"Main" bundle:nil];
        CommentsViewController *commentsViewController = [storyboard instantiateViewControllerWithIdentifier:@"Comments"];
        commentsViewController.msg_id=_msg_id;


        if ([commentsViewController respondsToSelector:@selector(setPreferredContentSize:)]) {
            commentsViewController.preferredContentSize = CGSizeMake(300, 500);             // iOS 7
        }
        else {
            commentsViewController.contentSizeForViewInPopover = CGSizeMake(300, 500);      // iOS < 7
        }


        commentsViewController.title = @"Comments";
        [commentsViewController.navigationItem setRightBarButtonItem:[[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemDone target:self action:@selector(close:)]];

        commentsViewController.modalInPopover = NO;

        UINavigationController* contentViewController = [[UINavigationController alloc] initWithRootViewController:commentsViewController];

        commentsPopoverController = [[WYPopoverController alloc] initWithContentViewController:contentViewController];
        commentsPopoverController.delegate = self;
        commentsPopoverController.passthroughViews = @[btn];
        commentsPopoverController.popoverLayoutMargins = UIEdgeInsetsMake(10, 10, 10, 10);
        commentsPopoverController.wantsDefaultContentAppearance = NO;

        [commentsPopoverController presentPopoverFromRect:btn.bounds
                                                   inView:btn
                                 permittedArrowDirections:WYPopoverArrowDirectionNone
                                                 animated:YES
                                                  options:WYPopoverAnimationOptionFadeWithScale];


    }


    else
    {
        [self close:nil];
    }
}
- (BOOL)popoverControllerShouldDismissPopover:(WYPopoverController *)controller
{
    return YES;
}

- (void)popoverControllerDidDismissPopover:(WYPopoverController *)controller
{
    if (controller == commentsPopoverController)
    {
        commentsPopoverController.delegate = nil;
        commentsPopoverController = nil;
    }
}



@end