Last TableView未执行

时间:2017-03-22 04:29:32

标签: ios objective-c uitableview

我已经在这几个小时了。出于某种原因,我方法中的最后一个tableview(neighboursView)没有执行。一些上下文:我在一个视图控制器中有3个表视图 - 前两个正确加载,但是如果tableView == neighboursView从不执行。请参阅下面的代码 - 感谢任何帮助!

ConnectViewController.h

@interface ConnectViewController : UIViewController <UITableViewDataSource, UITableViewDelegate>

- (IBAction)segmentControl:(id)sender;
@property (weak, nonatomic) IBOutlet UISegmentedControl *segmentControl;
@property (weak, nonatomic) IBOutlet UITableView *sidetableView;
@property (strong, nonatomic) NSMutableArray *myFriendData;
@property (weak, nonatomic) IBOutlet UITableView *neighboursView;
@property (strong, nonatomic) NSMutableArray *friendData;
@property (strong, retain) NSMutableArray *neighbourData;

@property (strong, nonatomic) IBOutlet UITableView *friendsView;

ConnectViewController.m

        - (void)viewDidLoad {
        [super viewDidLoad];

        UINib *nib = [UINib nibWithNibName:@"sidebarCell" bundle:nil];
        [self.friendsView registerNib:nib forCellReuseIdentifier: @"sidebarCell"];
        [self.neighboursView registerNib:nib forCellReuseIdentifier: @"sidebarCell"];

        nib = [UINib nibWithNibName:@"MyFriendTableViewCell" bundle:nil];
        [self.sidetableView registerNib:nib forCellReuseIdentifier: @"MyFriendTableViewCell"];

    }

    - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
    {
        // Return the number of sections.
        return 1;
    }

    - (NSInteger) tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
    {


        if (tableView == self.friendsView) {
        return [self.friendData count];

        }

        if (tableView == self.neighboursView) {
        return [self.neighbourData count];
        }

        if (tableView == self.sidetableView) {
        return [self.myFriendData count];

        } else {


            return 0; }
    }


    - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
    {

        if (tableView == self.sidetableView) {

            self.sidetableView.separatorStyle = UITableViewCellSeparatorStyleNone;


        MyFriendTableViewCell *cell = (MyFriendTableViewCell *)[tableView dequeueReusableCellWithIdentifier:@"MyFriendTableViewCell"];



            NSDictionary *friendsName = [self.myFriendData objectAtIndex:indexPath.row];
            [[cell friendName] setText:[friendsName objectForKey:@"title"]];

            NSDictionary *friendsBio = [self.myFriendData objectAtIndex:indexPath.row];
            [[cell friendBio] setText:[friendsBio objectForKey:@"field_friendbio"][@"und"][0][@"safe_value"]];


            NSString *profilePath = [[self.myFriendData objectAtIndex:indexPath.row] objectForKey:@"field_picture"][@"und"][0][@"safe_value"];


            [cell.friendPic sd_setImageWithURL:[NSURL URLWithString:profilePath]];

            NSLog(@"This is profilePath %@",profilePath);

        return cell;

    }

      else if (tableView == self.friendsView)

        {
            self.friendsView.separatorStyle = UITableViewCellSeparatorStyleNone;


            sidebarCell *cell = (sidebarCell *)[tableView dequeueReusableCellWithIdentifier:@"sidebarCell"];


            NSDictionary *userName = [self.friendData objectAtIndex:indexPath.row];
            [[cell username] setText:[userName objectForKey:@"node_title"]];


            NSDictionary *userBio = [self.friendData objectAtIndex:indexPath.row];
            [[cell userDescription] setText:[userBio objectForKey:@"body"]];

            NSString *profilePath = [[self.friendData objectAtIndex:indexPath.row] objectForKey:@"friendphoto"];

            [cell.usermini sd_setImageWithURL:[NSURL URLWithString:profilePath]];

            return cell;



        } else if (tableView == self.neighboursView)

        {

     sidebarCell *cell = (sidebarCell *)[tableView dequeueReusableCellWithIdentifier:@"sidebarCell"];


            NSDictionary *userName = [self.neighbourData objectAtIndex:indexPath.row];
            [[cell username] setText:[userName objectForKey:@"first name"]];

            NSDictionary *userlast = [self.neighbourData objectAtIndex:indexPath.row];
            [[cell lastName] setText:[userlast objectForKey:@"last name"]];

            NSDictionary *userBio = [self.neighbourData objectAtIndex:indexPath.row];
            [[cell userDescription] setText:[userBio objectForKey:@"userbio"]];



            NSString *profilePath = [[self.neighbourData objectAtIndex:indexPath.row] objectForKey:@"photo_path"];

            [cell.usermini sd_setImageWithURL:[NSURL URLWithString:profilePath]];

            NSLog(@"This is profilePath %@",profilePath);



            return cell;


        } else {

      UITableViewCell *cell;
     return cell;



    }
    }

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (tableView == self.sidetableView) {


        return 100;

    }

    if (tableView == self.friendsView) {

        return 115;

    }

    if (tableView == self.neighboursView) {

        return 115;

    }

    return 100;
}


    - (IBAction)segmentControl:(id)sender {

        UISegmentedControl *segmentedControl = (UISegmentedControl *) sender;
        NSInteger selectedSegment = segmentedControl.selectedSegmentIndex;

        switch (selectedSegment)
            {


                case 0:

                    [self.sidetableView setHidden:NO];
                   [self.neighboursView setHidden:YES];
                   [self.friendsView setHidden:YES];

                    NSLog(@"Requests!");

                    break;



                case 1: //FRIENDS MAP VIEW



                    [self.sidetableView setHidden:YES];
                   [self.friendsView setHidden:NO];
                  [self.neighboursView setHidden:YES];


                         NSLog(@"Friends!");

                    break;



                case 2:

                    [self.sidetableView setHidden:YES];
                   [self.neighboursView setHidden:NO];
                  [self.friendsView setHidden:YES];


                         NSLog(@"Neighbours!");
                    break;




            }



        }

3 个答案:

答案 0 :(得分:0)

问题出在这里

您还必须为neighboursView注册nib。您为两个表注册1个笔尖,见下文

Wrong
    UINib *nib = [UINib nibWithNibName:@"sidebarCell" bundle:nil];
        [self.friendsView registerNib:nib forCellReuseIdentifier: @"sidebarCell"];
        [self.neighboursView registerNib:nib forCellReuseIdentifier: @"sidebarCell"];

正确

neighboursView再创建一个nib,然后将其注册到neighboursView

 UINib *nib = [UINib nibWithNibName:@"sidebarCell" bundle:nil];
        [self.friendsView registerNib:nib forCellReuseIdentifier: @"sidebarCell"];

UINib *nib1 = [UINib nibWithNibName:@"sidebarCell" bundle:nil];// register your cell here for neighboursView
        [self.neighboursView registerNib:nib1 forCellReuseIdentifier: @"sidebarCell1"];// give your identifier here for neighboursView

答案 1 :(得分:0)

尝试更改:

@property (strong, retain) NSMutableArray *neighbourData;

@property (strong, nonatomic) NSMutableArray *neighbourData;

如果不是这样,我仍然认为它可能是你的nib / storyboard文件中的错误引用。即您将笔尖中的表链接到错误的属性。尝试在nib中选择邻居表,然后检查Connections Inspector选项卡。

Connections Inspector

答案 2 :(得分:0)

在加载self.neighboursView的数据后,您需要致电[self.neighboursView reloadData]。它将执行tableView dataSource个方法。

我想指出的一件事:如果你想访问一个数组元素,它不需要像你那样多次使用该元素的地址:

NSDictionary *userName = [self.friendData objectAtIndex:indexPath.row];
[[cell username] setText:[userName objectForKey:@"node_title"]];


NSDictionary *userBio = [self.friendData objectAtIndex:indexPath.row];
[[cell userDescription] setText:[userBio objectForKey:@"body"]];

NSString *profilePath = [[self.friendData objectAtIndex:indexPath.row] objectForKey:@"friendphoto"];

以上所有内容将成为:

NSDictionary *userData = [self.friendData objectAtIndex:indexPath.row];
[[cell username] setText:[userData objectForKey:@"node_title"]];
[[cell userDescription] setText:[userData objectForKey:@"body"]];
NSString *profilePath = [userData objectForKey:@"friendphoto"];

当然,在访问存储在其中的数据之前,您应检查[userData objectforkey:]是否为零,以避免崩溃。