• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    迪恩网络公众号

ios - 按创建日期组织 UITableViewCell

[复制链接]
菜鸟教程小白 发表于 2022-12-13 05:35:27 | 显示全部楼层 |阅读模式 打印 上一主题 下一主题

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

我在想我会从每个返回 created_at 值并将字符串转换为 NSDate 的,但我将如何为每条推文和 instapic 执行此操作?



Best Answer-推荐答案


出于您的目的,您必须创建一个新数组,将推文和 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 *)tableViewUITableView *)tableView cellForRowAtIndexPathNSIndexPath *)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;
}

在得到图片数组中的数据后重新加载你的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;

关于ios - 按创建日期组织 UITableViewCell,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20319528/

回复

使用道具 举报

懒得打字嘛,点击右侧快捷回复 【右侧内容,后台自定义】
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

关注0

粉丝2

帖子830918

发布主题
阅读排行 更多
广告位

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap