0

I'm having an issue with my UITableviewCell, it generates only the first index inside of my NSMutableArray. I've used NSLog(@"Count %i %@", [enterPrise_names count], enterPrise_names); To check the number of objects inside of my NSMutableArray everything seems fine with that. I've already wired up everything including the UItableViewDataSource, UITableViewDelege, and Matching up the cell identifier of the TableViewCell.

The problem is that two of my labels only show the first object inside of my NSMutableArrays. I want to post the photos of my simulation to you guys but this is my first post of Stackoverflow and I don't have enough reputation to do that task.

well it looks like this

Fuji Siam

Fuji Siam

Fuji Siam

Fuji Siam

Fuji Siam

Fuji Siam

Here is my code

#import "MyQueueViewController.h"
#import "QueueCell.h"
@interface MyQueueViewController ()

@end

@implementation MyQueueViewController

- (id)initWithStyle:(UITableViewStyle)style
{
    self = [super initWithStyle:style];
    if (self) {
        // Custom initialization

    }
    return self;
}
- (void)viewDidAppear:(BOOL)animated
{


}

- (void)viewDidLoad
{
    [super viewDidLoad];


     enterPrise_names = [[NSMutableArray alloc]initWithObjects:@"Fuji",@"MK",@"KFC",@"PizzaHut",@"McDonal",@"BurgerKing", nil];
    BranchName = [[NSMutableArray alloc]initWithObjects:@"Siam",@"Paragon", @"CTW", @"Pinklao", @"Bangkae", @"Bangna", nil];
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return [enterPrise_names count];
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return 1;

}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    static NSString *MyIdentifier = @"QueueCell";
    QueueCell *cell = [tableView dequeueReusableCellWithIdentifier:MyIdentifier];
    if (cell == nil) {
        cell = [[QueueCell alloc] initWithStyle:UITableViewCellStyleDefault  reuseIdentifier:MyIdentifier];

    }
    NSLog(@"Count %i %@", [enterPrise_names count], enterPrise_names);
    cell.txtBranch.text = [BranchName objectAtIndex:indexPath.row];
    cell.txtShop.text = [enterPrise_names objectAtIndex:indexPath.row];

    return cell;
}

I would be very appreciated if any of you guys would point out my mistakes. Thank you very much for your help.

2 Answers 2

1

Your tableview contains many sections but but only one row by section. Try this :

cell.txtBranch.text = [BranchName objectAtIndex:indexPath.section];
cell.txtShop.text = [enterPrise_names objectAtIndex:indexPath.section];

However, if you don't actually need many sections, you should probably set the number of rows according to your array and inverse numbers of rows and sections in here :

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return [enterPrise_names count];
}
Sign up to request clarification or add additional context in comments.

3 Comments

Yeahhhhh it works !!! Thank you very much I overlooked that Very appreciated Justafinger, you are the best !
Do you know how can I close this issue ? Since I already got the solution. I'm very new here.
You don't actually close issues, you just accept answers. In case other people have the same problem they'll be able to see what solved it.
0

Try, Change cellForRowAtIndexPath: method to

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    ....
    NSLog(@"Count %i %@", [enterPrise_names count], enterPrise_names);
    cell.txtBranch.text = [BranchName objectAtIndex:indexPath.section];
    cell.txtShop.text = [enterPrise_names objectAtIndex:indexPath.section];

    return cell;
}

Since each section has one row, indexPath.row will be zero in all case. So only first element of both BranchName and enterPrise_names array will be shown in labels. Changing indexPath.row to indexPath.section will solve the problem

2 Comments

Thank you very much for your help. I overlooked this point - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView { return [enterPrise_names count]; } - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { return 1; } if I just swap the return values of these two then my code will work just fine. ^^ Thank you again
@user3027109 Yes, you are correct. Changing it will solve the issue

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.