菜鸟教程小白 发表于 2022-12-13 05:35:27

ios - 按创建日期组织 UITableViewCell


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

<p>我在想我会从每个返回 <code>created_at</code> 值并将字符串转换为 <code>NSDate</code> 的,但我将如何为每条推文和 instapic 执行此操作?</p></p>
                                    <br><hr><h1><strong>Best Answer-推荐答案</ strong></h1><br>
                                            <p><p>出于您的目的,您必须创建一个新数组,将推文和 instaPics 数组合并为一个数组,然后将所有数据排序为单个数组。 </p>

<p>合并两个数据数组如下::</p>

<pre><code>NSArray *pictureArray = ;
</code></pre>

<p><strong>注意::</strong> <code>NSDictionary (NSMutableDictionary)</code> 默认按照其键值排序。这里我们根据包含字典的特定键对数组进行排序</p>

<p>现在将您的 <strong>pictureArray</strong> 排序如下::</p>

<pre><code>NSSortDescriptor *sortDescriptor = [ initWithKey:@&#34;created_at&#34; ascending:YES];
NSArray *sortDescriptors = ;
pictureArray = ;   
</code></pre>

<p>然后将您的 tableview 方法重新定义为::</p>

<pre><code>- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    static NSString *tweetsCellIdentifier = @&#34;tweetsCell&#34;;
    static NSString *instaCellIdentifier = @&#34;instaCell&#34;;
    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;
    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 = ;
    } else {
       cell = ;
    }

    if (cell == nil) {
      // Design your cell as you desired;
      if (tweets) {
      // Design cell for tweets
      cell = [ initWithStyle:UITableViewCellStyleDefault reuseIdentifier:tweetsCellIdentifier];
      } else {
      // Design cell for instagram
      cell = [ 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;
}
</code></pre>

<p>在得到图片数组中的数据后重新加载你的tableview如下..</p>

<pre><code>//Reload TableView
;
</code></pre>

<p>也许对你有帮助。</p>

<p><strong>已编辑::</strong>
如下编辑您的代码。我修改了您的 instgram 单元格创建和填充。像这样定义您的推文单元格设计和为 instagram 完成的填充数据</p>

<pre><code>NSUserDefaults *user = ;
    static NSString *tweetsCellIdentifier = @&#34;tweetsCell&#34;;
    static NSString *instaCellIdentifier = @&#34;instaCell&#34;;
    UITableViewCell *cell = nil;
    BOOL tweets = YES;
    BOOL 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;

// Get cell according to your dictionary data that may be from tweets or instagram
if (tweets) {
    cell = ;
} else {
    cell = ;
}

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&#39;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 = [ initWithStyle:UITableViewCellStyleDefault reuseIdentifier:tweetsCellIdentifier];

      cell.backgroundColor = ];

      NSDictionary *tweet = totalFeed;

      //Set username for twitter
      NSString *name = [ objectForKey:@&#34;name&#34;];
      UILabel *twitterNameLabel = (UILabel *);
      ];
      ;


      //Set status for twitter
      NSString *text = ;
      UILabel *twitterTweetLabel = (UILabel *);
      ];
      ;


      //Set Profile Pic for twitter
      dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
            NSString *imageUrl = [ objectForKey:@&#34;profile_image_url&#34;];
            NSData *data = ];

            dispatch_async(dispatch_get_main_queue(), ^{
                UIImageView *profilePic = (UIImageView *);
                profilePic.image = ;

                //Make the Profile Pic ImageView Circular
                CALayer *imageLayer = profilePic.layer;
                ;
                ;
            });
      });


      //Set number of Favorites for Tweet
      NSString *favoritesCount = [objectForKey:@&#34;favourites_count&#34;];
      UIButton *favoritesButton = (UIButton *);
       forState:UIControlStateNormal];
       forState:UIControlStateHighlighted];
      favoritesButton.titleLabel.font = ;

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

      cell.backgroundColor = ];

      UIImageView *instagramImageView = [ init];
      instagramImageView.tag = 104;
         ;

      UILabel *instagramUserLabel = [ init];
      instagramUserLabel.tag = 102;
      ];
      ;

      UILabel *instagramCaptionLabel = [ init];
            ];
      instagramCaptionLabel.tag = 103;
         ;

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

      ;
    }

}

// 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;

   NSString *imageUrlString = entry[@&#34;images&#34;][@&#34;low_resolution&#34;][@&#34;url&#34;];
   NSURL *url = ;
      UIImageView *instagramImageView = (UIImageView *);
      ;

      NSString *user =entry[@&#34;user&#34;][@&#34;full_name&#34;];
      UILabel *instagramUserLabel = (UILabel *);
      ;

   UILabel *instagramCaptionLabel = (UILabel *);
   if (entry[@&#34;caption&#34;] != &amp;&amp; entry[@&#34;caption&#34;][@&#34;text&#34;] != )            {
            NSString *caption = entry[@&#34;caption&#34;][@&#34;text&#34;];
            ;
      }else{
            NSString *caption = @&#34;&#34;;
            ;

      }

   NSString *imageUserPicUrl = entry[@&#34;user&#34;][@&#34;profile_pic&#34;][@&#34;url&#34;];
   NSURL *profileURL = ;
   UIImageView *instagramProfilePic = (UIImageView *);
   ;
}

return cell;
</code></pre></p>
                                   
                                                <p style="font-size: 20px;">关于ios - 按创建日期组织 UITableViewCell,我们在Stack Overflow上找到一个类似的问题:
                                                        <a href="https://stackoverflow.com/questions/20319528/" rel="noreferrer noopener nofollow" style="color: red;">
                                                                https://stackoverflow.com/questions/20319528/
                                                        </a>
                                                </p>
                                       
页: [1]
查看完整版本: ios - 按创建日期组织 UITableViewCell