2013년 12월 12일 목요일

[iOS] tableview dynamic height

//
//  MainTableViewController.m
//  mifd
//
//  Created by 이종현 on 2013. 11. 20..
//  Copyright (c) 2013년 belhyun. All rights reserved.
//

#import "MainTableViewController.h"
#import "MainTableViewCell.h"
#import "HttpClient.h"
#import "AppDelegate.h"

#define FONT_SIZE 14.0f
#define CELL_CONTENT_WIDTH 320.0f
#define CELL_CONTENT_MARGIN 5.0f
#define CELL_EXTRA_AREA 60.0f;

const int kLoadingCellTag = 1273;
@interface MainTableViewController ()
@property(nonatomic,assign) Boolean isExpand;
@property (nonatomic, retain) NSMutableArray *tweets;
-(void)pullToRefresh;
-(void)stopRefresh;
-(void)scrollToTop;
-(void)retweetButtonPressed:(id)sender;
-(void)favoriteButtonPressed:(id)sender;
-(void)retweetDelButtonPressed:(id)sender;
-(void)favoriteDelButtonPressed:(id)sender;
-(void)snsRequest:(NSString *)url :(id)sender :(NSMutableDictionary *)params :(NSString *)type :(void (^)(void))callbackBlock;
-(void)mifdRequest:(NSMutableDictionary *)params :(NSUInteger) rowId;
@end
@implementation MainTableViewController

- (id)initWithStyle:(UITableViewStyle)style
{
    self = [super initWithStyle:style];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Uncomment the following line to preserve selection between presentations.
    // self.clearsSelectionOnViewWillAppear = NO;
 
    // Uncomment the following line to display an Edit button in the navigation bar for this view controller.
    // self.navigationItem.rightBarButtonItem = self.editButtonItem;
    self.tableView.separatorColor = [UIColor yellowColor];
    self.tableView.contentInset = UIEdgeInsetsMake(0, 0, 10, 0);
    UITabBarItem *tabBarItem = [self.tabBarController.tabBar.items objectAtIndex:0];
    tabBarItem.image = [UIImage imageNamed:@"twitter_thumb.png"];
    self.tweets = [[NSMutableArray alloc]init];
    self.isExpand = false;
    self.curPage = 1;
    self.HUD = [[MBProgressHUD alloc] initWithView:self.view];
    [self.view addSubview:self.HUD];
    self.HUD.delegate = self;
    [self.HUD show:YES];
    UIRefreshControl *refresh = [[UIRefreshControl alloc]init];
    refresh.attributedTitle = [[NSAttributedString alloc]initWithString:@"Pull to refresh" attributes:nil];
    [refresh addTarget:self action:@selector(pullToRefresh) forControlEvents:UIControlEventValueChanged];
    self.refreshControl = refresh;
    self.tableView.dataSource = self;
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(fetchTweetsWithInit) name:@"fetchTweets" object:nil];
    [self fetchTweets];
}

-(void)pullToRefresh{
    self.curPage = 1;
    self.tweets = [[NSMutableArray alloc]init];
    [self fetchTweets];
    [self performSelector:@selector(stopRefresh) withObject:nil afterDelay:1.5];
}

-(void)stopRefresh{
    [self.refreshControl endRefreshing];
}

-(void) scrollToTop
{
    if ([self numberOfSectionsInTableView:self.tableView] > 0)
    {
        NSIndexPath* top = [NSIndexPath indexPathForRow:NSNotFound inSection:0];
        [self.tableView scrollToRowAtIndexPath:top atScrollPosition:UITableViewScrollPositionTop animated:YES];
    }
}

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

#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    // Return the number of sections.
    if(self.curPage < self.totalPage){
        return self.tweets.count + 1;
    }
    return self.tweets.count;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    // Return the number of rows in the section.
    return 1;
}

-(void)changeLoginView{
    [[[UIAlertView alloc] initWithTitle:@"MIFD" message:@"트위터 로그인이 필요합니다." delegate:self cancelButtonTitle:@"확인" otherButtonTitles:nil, nil] show];
    self.tabBarController.selectedViewController = [self.tabBarController.viewControllers objectAtIndex:1];
}

-(void)changeTweetView{
    self.tabBarController.selectedViewController = [self.tabBarController.viewControllers objectAtIndex:0];
}

- (Boolean)isDefinedEle:(NSArray *)array :(NSInteger)tag{
    NSEnumerator *enumerator = [array objectEnumerator];
    id anObject;
    if([array count] == 0) return false;
    while (anObject = [enumerator nextObject]) {
        if(((UIView *)anObject).tag == tag){
            return  true;
        }
    }
    return false;
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    UIView *headerView = [[UIView alloc] init];
    headerView.backgroundColor = [UIColor clearColor];
    return headerView;
    /*
     UIView *headerView = [[UIView alloc] initWithFrame:CGRectMake(0,0,tableView.frame.size.width,30)];
     
     UILabel *headerLabel = [[UILabel alloc] initWithFrame:CGRectMake(60, 0, headerView.frame.size.width-120.0, headerView.frame.size.height)];
     
     headerLabel.textAlignment = UITextAlignmentRight;
     headerLabel.backgroundColor = [UIColor clearColor];
     NSInteger tbHeight = 50;
     UIToolbar *tb = [[UIToolbar alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, tbHeight)];
     tb.translucent = YES;
     UIBarButtonItem *flexibleSpace = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil];
     UIBarButtonItem *doneButton = [[UIBarButtonItem alloc] initWithTitle:@"완료" style:UIBarButtonItemStyleBordered target:self action:@selector(completeSelect)];
     NSArray *barButton  =   [[NSArray alloc] initWithObjects:flexibleSpace,doneButton,nil];
     [tb setItems:barButton];
     [headerView addSubview:tb];
     barButton = nil;
     [headerView addSubview:headerLabel];
     
     return headerView;
     */
}

-(void) expandRow:(UITapGestureRecognizer *)gr{
    /*
    MainTableViewCell *view = (MainTableViewCell *)gr.view;
    if(self.isExpand){
        self.isExpand = false;
     }else{
     self.isExpand = true;
     }
     [self.tableView beginUpdates];
     [self.tableView endUpdates];
     */
}

-(void) fetchTweetsWithInit{
    self.curPage = 1;
    self.tweets = [[NSMutableArray alloc]init];
    [self fetchTweets];
}

-(void) fetchTweets{
    HttpClient *httpClient = [HttpClient sharedClient];
    [httpClient GET:[NSMutableString stringWithFormat:@"%@?page=%d&user_desc=%@",RANK,self.curPage,[User getUserDesc]] parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject) {
        [self.HUD hide:YES];
        self.totalPage = [[responseObject objectForKey:@"total_page"] intValue];
        self.totalCount = [[responseObject objectForKey:@"total_count"] intValue];
        responseObject = [responseObject objectForKey:@"tweets"];
        for(id tweetDictionary in responseObject){
            Tweet *tweet = [[Tweet alloc] initWithDictionary:tweetDictionary];
            [self.tweets addObject:tweet];
        }
        [self.tableView reloadData];
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        [self.HUD hide:YES];
        NSLog(@"Error: %@", error);
    }];
}

-(NSIndexPath *)tableView:(UITableView *)tableView willSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    return nil;
}

-(void)retweetButtonPressed:(id)sender{
    UIButton *clicked = (UIButton *) sender;
    if([User isLogged]){
        [self.HUD show:true];
        [self snsRequest:[NSString stringWithFormat:@"https://api.twitter.com/1.1/statuses/retweet/%@.json",((Tweet *)[self.tweets objectAtIndex:clicked.tag]).uuid] :sender :nil :@"R" :^(void){
            NSMutableDictionary *params = [[NSMutableDictionary alloc]init];
            [params setObject:[MifdKeychainItemWrapper keychainStringFromMatchingIdentifier:@"desc"] forKey:@"user_desc"];
            [params setObject:((Tweet *)[self.tweets objectAtIndex:clicked.tag]).uuid forKey:@"tweet_uuid"];
            [params setObject:@"R" forKey:@"type"];
            [self mifdRequest:params :clicked.tag];
        }];
    }else{
        [self changeLoginView];
    }
}

-(void)favoriteButtonPressed:(id)sender{
    UIButton *clicked = (UIButton *) sender;
    if([User isLogged]){
        [self.HUD show:true];
        NSMutableDictionary *dictionary = [[NSMutableDictionary alloc]init];
        [dictionary setObject:((Tweet *)[self.tweets objectAtIndex:clicked.tag]).uuid forKey:@"id"];
        [self snsRequest:@"https://api.twitter.com/1.1/favorites/create.json" :sender :dictionary :@"F" :^(void){
            NSMutableDictionary *params = [[NSMutableDictionary alloc]init];
            [params setObject:[MifdKeychainItemWrapper keychainStringFromMatchingIdentifier:@"desc"] forKey:@"user_desc"];
            [params setObject:((Tweet *)[self.tweets objectAtIndex:clicked.tag]).uuid forKey:@"tweet_uuid"];
            [params setObject:@"F" forKey:@"type"];
            [self mifdRequest:params :clicked.tag];
        }];
    }else{
        [self changeLoginView];
    }
}

-(void)retweetDelButtonPressed:(id)sender{
    if([User isLogged]){
        [self.HUD hide:true];
        [[[UIAlertView alloc] initWithTitle:@"MIFD" message:@"이미 retweet 하셨네요!" delegate:self cancelButtonTitle:@"확인" otherButtonTitles:nil, nil] show];
    }else{
        [self changeLoginView];
    }
}

-(void)favoriteDelButtonPressed:(id)sender{
    if([User isLogged]){
        [self.HUD hide:true];
        [[[UIAlertView alloc] initWithTitle:@"MIFD" message:@"이미 favorite 하셨네요!" delegate:self cancelButtonTitle:@"취소" otherButtonTitles:nil, nil] show];
    }else{
        [self changeLoginView];
    }
}

-(void)mifdRequest:(NSMutableDictionary *)params :(NSUInteger)tag{
    HttpClient *httpClient = [HttpClient sharedClient];
    [httpClient POST:[NSMutableString stringWithFormat:@"%@",USER_TWEET] parameters:params success:^(AFHTTPRequestOperation *operation, id responseObject) {
        [self.HUD hide:YES];
        if([[responseObject objectForKey:@"result"] integerValue] == 1){
            UserTweet *userTweet = [[UserTweet alloc]init];
            userTweet.tweetUuid = [params objectForKey:@"tweet_uuid"];
            userTweet.userDesc = [User getUserDesc];
            if([[params objectForKey:@"type"] isEqualToString:@"F"]){
                userTweet.type = @"F";
            }else if([[params objectForKey:@"type"] isEqualToString:@"R"]){
                userTweet.type = @"R";
            }
            [((Tweet *)[self.tweets objectAtIndex:tag]).userTweets addObject:userTweet];
            [self.tableView reloadData];
        }
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        [self.HUD hide:YES];
        NSLog(@"Error: %@", error);
    }];
}


-(void)snsRequest:(NSString *)url :(id)sender :(NSMutableDictionary *)params :(NSString *)type :(void (^)(void))callbackBlock{
    ACAccountStore *accountStore = [[ACAccountStore alloc]init];
    ACAccountType *accountType = [accountStore accountTypeWithAccountTypeIdentifier:ACAccountTypeIdentifierTwitter];
    [self.HUD show:YES];
    if([MifdKeychainItemWrapper keychainStringFromMatchingIdentifier:@"desc"] != nil){
        NSArray *accountsArray = [accountStore accountsWithAccountType:accountType];
        if ([accountsArray count] > 0) {
            ACAccount *twitterAccount = [accountsArray objectAtIndex:0];
            NSURL *requestUrl = [NSURL URLWithString:url];
            SLRequest *posts = [SLRequest requestForServiceType:SLServiceTypeTwitter requestMethod:SLRequestMethodPOST URL:requestUrl parameters:params];
            [posts setAccount:twitterAccount];
            [posts performRequestWithHandler:^(NSData *responseData, NSHTTPURLResponse *urlResponse, NSError *error) {
                callbackBlock();
            }];
        }
    }else{
        //로그인이 되어있지 않을 때
    }
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"tweet";
    UITableViewCell *cell = nil;
    
    if(indexPath.section >= self.tweets.count){
        return [self loadingCell];
    }
    
    cell = [self.tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
    if(cell == nil){
        cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier];
    }
    
    if ((([cell.contentView viewWithTag:1])))
    {
        [[cell.contentView viewWithTag:1]removeFromSuperview];
    }
    Tweet *tweet = [self.tweets objectAtIndex:indexPath.section];
    MainTableViewCell *subCell = [[MainTableViewCell alloc]init];
    subCell.itemId = indexPath.section;
    [subCell setFrame:CGRectMake(10, 0, cell.contentView.bounds.size.width-18, cell.bounds.size.height)];
    subCell.backgroundColor = [UIColor yellowColor];
    [subCell setTag:indexPath.section];
    subCell.tag = 1;
    //UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(expandRow:)];
    //[subCell addGestureRecognizer:tap];
    [cell.contentView addSubview:subCell];
    
    TTTAttributedLabel *text = [[TTTAttributedLabel alloc] init];
    text.delegate = self;
    text.enabledTextCheckingTypes = NSTextCheckingTypeLink;
    text.text = (NSString *)[[self getText:tweet] mutableCopy];
    [text setNumberOfLines:0];
    [text setFrame:CGRectMake(60, 0, cell.contentView.bounds.size.width-85, cell.bounds.size.height)];
    //[text setBackgroundColor:[UIColor redColor]];
    [text sizeToFit];
    [[subCell contentView] addSubview:text];
    
    UIImageView * imageView = [[UIImageView alloc] init];
    [imageView setFrame:CGRectMake(0, 0, 50.0, 50.0)];
    [[subCell contentView] addSubview:imageView];
    [HttpClient downloadingServerImageFromUrl:imageView AndUrl:tweet.user.image];
    
    
    UIButton *retweetBtn = [[UIButton alloc]initWithFrame:CGRectMake(55.0, text.frame.size.height+((cell.bounds.size.height-text.frame.size.height)/6.0), 30.0, 30.0)];
    [retweetBtn setReversesTitleShadowWhenHighlighted:YES];
    [retweetBtn setShowsTouchWhenHighlighted:YES];
    retweetBtn.tag = indexPath.section;
    [retweetBtn setBackgroundImage:[UIImage imageNamed:@"twitter_retweet"] forState:UIControlStateNormal];
    if(![self isAlreadyRetweet:tweet]){
        [retweetBtn addTarget:self action:@selector(retweetButtonPressed:) forControlEvents:UIControlEventTouchUpInside];
    }else{
        [retweetBtn addTarget:self action:@selector(retweetDelButtonPressed:) forControlEvents:UIControlEventTouchUpInside];
    }
    [subCell.contentView addSubview:retweetBtn];
    
    UIButton *favoriteBtn = [[UIButton alloc]initWithFrame:CGRectMake(110.0, text.frame.size.height+((cell.bounds.size.height-text.frame.size.height)/6.0), 30.0, 30.0)];
    [favoriteBtn setReversesTitleShadowWhenHighlighted:YES];
    [favoriteBtn setShowsTouchWhenHighlighted:YES];
    favoriteBtn.tag = indexPath.section;
    [favoriteBtn setBackgroundImage:[UIImage imageNamed:@"favorite"] forState:UIControlStateNormal];
    if(![self isAlreadyFavorite:tweet]){
        [favoriteBtn addTarget:self action:@selector(favoriteButtonPressed:) forControlEvents:UIControlEventTouchUpInside];
    }else{
        [favoriteBtn addTarget:self action:@selector(favoriteDelButtonPressed:) forControlEvents:UIControlEventTouchUpInside];
    }
    [subCell.contentView addSubview:favoriteBtn];
    
    [cell setSelectionStyle:UITableViewCellSelectionStyleNone];
    [cell setSelected:NO animated:NO];
    cell.userInteractionEnabled = YES;
    return cell;
}

-(Boolean)isAlreadyRetweet:(Tweet *)tweet{
    if(tweet.userTweets == nil){
        return false;
    }
    for(NSUInteger i=0;i

댓글 없음:

댓글 쓰기