按创建日期组织UITableViewCell

时间:2013-12-02 01:33:54

标签: ios objective-c uitableview twitter instagram

我在两个单独的NSDictionaries中有推文和Instagram图片,目前我只是通过将每个其他帖子作为推文来聚合帖子,而后者是Instagram图片。我如何处理这两个并组织所有UITableViewCell并按日期组织它们?

我在想我会从每个值返回created_at值并将字符串转换为NSDate,但是我如何为每一条推文和instapic做到这一点?

3 个答案:

答案 0 :(得分:2)

在调用cellForRowAtIndexPath之前,您必须使用 creation_date 推文 instaPics 数组进行排序。在viewDidLoad中编写此代码,然后重新加载tableView。

NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc] initWithKey:@"created_at" ascending:YES];
NSArray *sortDescriptors = [NSArray arrayWithObject:sortDescriptor];
tweets = [tweets sortedArrayUsingDescriptors:sortDescriptors];
instaPics = [instaPics sortedArrayUsingDescriptors:sortDescriptors];
//Reload TableView
[tableView reloadData];

希望这会有所帮助:)

答案 1 :(得分:2)

为了您的目的,您必须创建一个新数组,将单个推文和instaPics数组合并,然后将所有数据排序为单个数据。

合并两个数据数组,如下所示::

NSArray *pictureArray = [tweets arrayByAddingObjectsFromArray: instaPics];

注意:: NSDictionary (NSMutableDictionary)默认情况下会根据其键值进行排序。这里我们根据包含词典的特定键对数组进行排序

现在将 pictureArray 排序如下::

NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc] initWithKey:@"created_at" ascending:YES];
NSArray *sortDescriptors = [NSArray arrayWithObject:sortDescriptor];
pictureArray = [pictureArray sortedArrayUsingDescriptors:sortDescriptors];    

然后将tableview方法重新定义为::

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    static NSString *tweetsCellIdentifier = @"tweetsCell";
    static NSString *instaCellIdentifier = @"instaCell";
    UITableViewCell *cell = nil;
    BOOL tweets = NO;

    // Now you get dictionary that may be of tweets array or instagram array 
    // Due to its different structure
    // I thinks your both dictionaries have different structure
    NSDictionary *pictDictionary = pictureArray[indexPath.row];
    if (your_check_condition for tweets dictionary) {
        tweets = YES;
    }

    // Get cell according to your dictionary data that may be from tweets or instagram
    if (tweets) {
       cell = [tableView dequeueReusableCellWithIdentifier:tweetsCellIdentifier];
    } else {
       cell = [tableView dequeueReusableCellWithIdentifier:instaCellIdentifier];
    }

    if (cell == nil) {
      // Design your cell as you desired;
      if (tweets) {
        // Design cell for tweets
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:tweetsCellIdentifier];
      } else {
        // Design cell for instagram
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:instaCellIdentifier];
      }

    }


     // Write your login to get dictionary picture data
     // Tweets and Instagram array are merged. So get appropriate data with your logic
     // May be both dictionaries structure are different. so write your logic to get picture data
      // Fill data according tweets dict or instgram
      // Get cell elements in which you will show dict data i.e. images, title etc.
      if (tweets) {
        // Fill cell data for tweets
      } else {
        // Fill cell data for instagram
      }

      return cell;
}

以上所有内容在获取pictureArray中的数据后重新加载tableview,如下所示。

//Reload TableView
[tableView reloadData];

它可能对你有帮助。

编辑:: 编辑您的代码如下。我修改你的instgram单元格创建和填充。像这样定义你的推文单元格设计和填充数据就像为instagram

NSUserDefaults *user = [NSUserDefaults standardUserDefaults];
    static NSString *tweetsCellIdentifier = @"tweetsCell";
    static NSString *instaCellIdentifier = @"instaCell";
    UITableViewCell *cell = nil;
    BOOL tweets = YES;
    BOOL twitterLoggedIn = [user boolForKey:@"twitterLoggedIn"];

// Now you get dictionary that may be of tweets array or instagram array
// Due to its different structure
// I thinks your both dictionaries have different structure
NSDictionary *totalFeedDictionary = totalFeed[indexPath.row];

// Get cell according to your dictionary data that may be from tweets or instagram
if (tweets) {
    cell = [tableView dequeueReusableCellWithIdentifier:tweetsCellIdentifier];
} else {
    cell = [tableView dequeueReusableCellWithIdentifier:instaCellIdentifier];
}

if (cell == nil) {
    // Design your cell as you desired;
    if (tweets) {
        // Now correct it as instagram cell design below your tweets cell design
        // Only create here desired elements and its define design pattern here
        // Don't Fill here At last already we fill it 
        // Due to scrolling cells are reuse and must be fill all time when they come from deque cel
        // Design cell for tweets
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:tweetsCellIdentifier];

        cell.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"Background.png"]];

        NSDictionary *tweet = totalFeed[indexPath.row];

        //Set username for twitter
        NSString *name = [[tweet objectForKey:@"user"] objectForKey:@"name"];
        UILabel *twitterNameLabel = (UILabel *)[cell viewWithTag:202];
        [twitterNameLabel setFont:[UIFont fontWithName:@"Helvetica-Light" size:12.0]];
        [twitterNameLabel setText:name];


        //Set status for twitter
        NSString *text = [tweet objectForKey:@"text"];
        UILabel *twitterTweetLabel = (UILabel *)[cell viewWithTag:203];
        [twitterTweetLabel setFont:[UIFont fontWithName:@"Helvetica-Light" size:10.0]];
        [twitterTweetLabel setText:text];


        //Set Profile Pic for twitter
        dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
            NSString *imageUrl = [[tweet objectForKey:@"user"] objectForKey:@"profile_image_url"];
            NSData *data = [NSData dataWithContentsOfURL:[NSURL URLWithString:imageUrl]];

            dispatch_async(dispatch_get_main_queue(), ^{
                UIImageView *profilePic = (UIImageView *)[cell viewWithTag:201];
                profilePic.image = [UIImage imageWithData:data];

                //Make the Profile Pic ImageView Circular
                CALayer *imageLayer = profilePic.layer;
                [imageLayer setCornerRadius:25];
                [imageLayer setMasksToBounds:YES];
            });
        });


        //Set number of Favorites for Tweet
        NSString *favoritesCount = [[tweet objectForKey:@"user"]objectForKey:@"favourites_count"];
        UIButton *favoritesButton = (UIButton *)[cell viewWithTag:204];
        [favoritesButton setTitle:[NSString stringWithFormat:@"  %@",favoritesCount] forState:UIControlStateNormal];
        [favoritesButton setTitle:[NSString stringWithFormat:@"  %@",favoritesCount] forState:UIControlStateHighlighted];
        favoritesButton.titleLabel.font = [UIFont fontWithName:@"Helvetica-Light" size:12];

    } else {
        // Design cell for instagram
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:instaCellIdentifier];

        cell.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"Background.png"]];

        UIImageView *instagramImageView = [[UIImageView alloc] init];
        instagramImageView.tag = 104;
         [cell.contentView addSubview:instagramImageView];

        UILabel *instagramUserLabel = [[UILabel alloc] init];
        instagramUserLabel.tag = 102;
        [instagramUserLabel setFont:[UIFont fontWithName:@"Helvetica-Light" size:16.0]];
        [cell.contentView addSubview:instagramUserLabel];

        UILabel *instagramCaptionLabel = [[UILabel alloc] init];
            [instagramCaptionLabel setFont:[UIFont fontWithName:@"Helvetica-Light" size:12.0]];
        instagramCaptionLabel.tag = 103;
         [cell.contentView addSubview:instagramCaptionLabel];

        UIImageView *instagramProfilePic = [[UIImageView alloc] init];
        instagramProfilePic.frame = CGRectMake(35, 31, 50, 50);
        instagramProfilePic.tag = 101;

        [cell.contentView addSubview:instagramProfilePic];
    }

}

// Fill Data here
if (tweets) {
     // Now correct as below your tweets cell filling
     // Only get here desired elements and fill them here
} else {
     NSDictionary *entry = totalFeed[indexPath.row];

     NSString *imageUrlString = entry[@"images"][@"low_resolution"][@"url"];
     NSURL *url = [NSURL URLWithString:imageUrlString];
        UIImageView *instagramImageView = (UIImageView *)[cell viewWithTag:104];
        [instagramImageView setImageWithURL:url];

        NSString *user =  entry[@"user"][@"full_name"];
        UILabel *instagramUserLabel = (UILabel *)[cell viewWithTag:102];
        [instagramUserLabel setText:user];

     UILabel *instagramCaptionLabel = (UILabel *)[cell viewWithTag:103];
     if (entry[@"caption"] != [NSNull null] && entry[@"caption"][@"text"] != [NSNull null])            {
            NSString *caption = entry[@"caption"][@"text"];
            [instagramCaptionLabel setText:caption];
        }else{
            NSString *caption = @"";
            [instagramCaptionLabel setText:caption];

        }

     NSString *imageUserPicUrl = entry[@"user"][@"profile_pic"][@"url"];
     NSURL *profileURL = [NSURL URLWithString:imageUserPicUrl];
     UIImageView *instagramProfilePic = (UIImageView *)[cell viewWithTag:101];
     [instagramProfilePic setImageWithURL:profileURL];
}

return cell;

答案 2 :(得分:0)

[self.tweets sortUsingComparator:^(Tweet *a, Tweet *b){
    return [b.dateCreated compare:a.dateCreated];
}];
相关问题