Tableview 未使用 Objective-c 显示 NewsFeed

Posted

技术标签:

【中文标题】Tableview 未使用 Objective-c 显示 NewsFeed【英文标题】:Tableview is not displaying the NewsFeed using objective-c 【发布时间】:2018-02-16 05:33:26 【问题描述】:

News feed 未显示在与 Newsfeed Viewcontroller 链接的 table viewcontroller.BuyerSocialPage 中具有 BuyerSocialPage.h 文件

@interface BuyerSocialPage : UIViewController <UITableViewDataSource,UITableViewDelegate>

@end

@implementation BuyerSocialPage

- (void)viewDidLoad 
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.tableView.delegate=self;
    UINib * firstNib = [UINib nibWithNibName:@"BSPFirstCell" bundle:nil];
    [self.tableView registerNib:firstNib forCellReuseIdentifier:@"BSPFirstCell"];
    UINib * secondNib = [UINib nibWithNibName:@"BSPSecondCell" bundle:nil];
    [self.tableView registerNib:secondNib forCellReuseIdentifier:@"BSPSecondCell"];
    UINib * thirdNib = [UINib nibWithNibName:@"BSPThirdCell" bundle:nil];
    [self.tableView registerNib:thirdNib forCellReuseIdentifier:@"BSPThirdCell"];
    UINib * fourthNib = [UINib nibWithNibName:@"BSPFourthCell" bundle:nil];
    [self.tableView registerNib:fourthNib forCellReuseIdentifier:@"BSPFourthCell"];
    self.view.backgroundColor = [UIColor whiteColor];
    [self getBuyerSocialPage];

    if (self.revealViewController) 
        [_sidebarButton addTarget:self.revealViewController action:@selector(revealToggle:) forControlEvents:UIControlEventTouchUpInside];
        [self.view addGestureRecognizer:self.revealViewController.panGestureRecognizer];
    



-(void)getBuyerSocialPage 

    



-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
    int row = (int)indexPath.row;
    if (row == 0) 

        return 324;
    
    else if (row == 1)
    
        return 152;
    
    else if (row == 2)
    
        return 152;
    
    else
    
        return 152;
    




@end

登录后,您将看到主屏幕。从顶部的侧面菜单栏中选择“新闻提要”,它应该显示新闻提要。但它没有显示新闻提要。Api 在邮递员上正确运行

如何在表格视图中获取新闻提要?

【问题讨论】:

请不要破坏您的帖子。通过在 Stack Exchange 网络上发布,您已授予 SE 分发该内容的不可撤销的权利(在 CC BY-SA 3.0 license 下)。根据 SE 政策,任何破坏行为都将被撤销。 【参考方案1】:

您的网址&amp;pageno=1 中缺少页码。

您的网址如下所示: http://api.shoclef.com/api/NewsFeed?user_id=1164

应该是这样的: http://api.shoclef.com/api/NewsFeed?user_id=1164&pageno=1

在你的 API Manager 类中试试这个。对我来说工作得很好。

 NSString * url = [NSString stringWithFormat:@"%@NewsFeed?user_id=%I&pageno=1",API_BASE_URL,userID];

图片更新

NewsFeedNew class 中的cellForRowAtIndexPath 函数中更新此代码

NSString *strUrl = [self.images objectAtIndex:indexPath.row].image;
    strUrl = [strUrl stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet URLFragmentAllowedCharacterSet]];
    [cell.image sd_setImageWithURL:[NSURL URLWithString:strUrl]];

【讨论】:

应该在哪里添加带有 url 的 pageno ? 嘿@aaaaaaaaaaaaaaaaaaaaaaaaaa。我更新答案请检查 它应该在表格视图的每个单元格的新闻提要上显示。为什么没有显示图像 @aaaaaaaaaaaaaaaaaaaaaaaaa 你的图片在这个网址上不存在。请与您的服务器人联系。 [链接](shoclef.com/Content/Images/Newsfeed/93Requests.jpg) 此网址正在显示图片。 shoclef.com/Content/Images/Newsfeed/96%20Requests.jpg ...如何在表格视图的每个单元格上显示【参考方案2】:

需要设置tableviewdelegatedataSource

还在numberOfRowsInSection 方法中设置一个断点,以验证tableview 是否设置了数据源和委托。

【讨论】:

我已经连接了数据源和委托,但表格视图控制器中仍然没有显示新闻源 “numberOfRowsInSection”是否被调用? 这就是问题...检查您的数据源数组....从中您正在显示数据....这将 array.count == 0。所以没有数据....在数组中......获取它然后重新加载tableview。【参考方案3】:

收到响应后需要重新加载tableview的数据 - [self.tableView reloadData];

-(void)getBuyerSocialPage 
    NSLog(@"getBuyerSocialPage");
    UserDao * profileID = [[DatabaseManager sharedManager]getLoggedInUser];
    ApiManager * manager = [ApiManager sharedManager];
    [manager socialPageWithProfileID:profileID.userID withCompletionBlock:^(BOOL error, NSDictionary *socialPage) 
    //    NSMutableArray * details = [[NSMutableArray alloc]init];
        for (NSDictionary * temp in socialPage ) 
            [self.socialPageArray addObject:temp];
        
        [self.tableView reloadData];
        if (!error) 
            self.profileImages=self.socialPageArray;
        
    ];

【讨论】:

我已经尝试过你的函数代码,但是表格视图仍然不显示新闻提要【参考方案4】:

这里要调试的东西很少,

您的 API 响应可能为零或有错误检查并记录适当的响应。

如果服务器响应正确,则进入主线程并重新加载 TableView

如果你还没有在 storyboard 中完成,请将 UITableViewDataSource 和 UITableViewDelegate 设置为 self。

- (void)getBuyerSocialPage 
    NSLog(@"getBuyerSocialPage");
    UserDao *profileID = [[DatabaseManager sharedManager] getLoggedInUser];
    ApiManager *manager = [ApiManager sharedManager];

    __weak BuyerSocialPage *weakSelf = self;
    [manager socialPageWithProfileID:profileID.userID withCompletionBlock:^(BOOL error, NSDictionary *socialPage)  [weak self]
        if (error) 
            NSLog("Error fetching data");
            return;
        

        for (NSDictionary *temp in socialPage ) 
            [weakSelf.socialPageArray addObject:temp];
        

        if ([weakSelf.socialPageArray count] > 0) 
            // update UI on the main thread
            dispatch_async(dispatch_get_main_queue(), ^
                weakSelf.tableView.reloadData()
            
        
     ]

【讨论】:

以上是关于Tableview 未使用 Objective-c 显示 NewsFeed的主要内容,如果未能解决你的问题,请参考以下文章

在现有的 tableview 中使用 Objective-C 实现 UISearchController

使用 Objective-C iOS 以编程方式创建 TableVIew

带数组的 TableView (Objective-C)

IOS/Objective-C:确定以编程方式创建的 tableview 中 imageView 的大小

Objective-C 链接两个 Tableview

ios objective-c tableView searchBar