简体   繁体   English

NSMutableArray与UITableViewCell

[英]NSMutableArray With UITableViewCell

I'm having an issue with my UITableviewCell , it generates only the first index inside of my NSMutableArray . 我的UITableviewCell出现问题,它仅在NSMutableArray内部生成第一个索引。 I've used NSLog(@"Count %i %@", [enterPrise_names count], enterPrise_names); 我用过NSLog(@“ Count%i%@”,[enterPrise_names count],enterPrise_names); To check the number of objects inside of my NSMutableArray everything seems fine with that. 要检查我的NSMutableArray内部的对象数量,一切似乎都很好。 I've already wired up everything including the UItableViewDataSource , UITableViewDelege , and Matching up the cell identifier of the TableViewCell. 我已经连接了所有东西,包括UItableViewDataSourceUITableViewDelege以及Matching TableViewCell的单元格标识符。

The problem is that two of my labels only show the first object inside of my NSMutableArrays . 问题是我的两个标签仅显示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. 我想将模拟的照片发布给大家,但这是我关于Stackoverflow的第一篇文章,我没有足够的声誉来完成该任务。

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. 非常感谢您的帮助。

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];
}

Try, Change cellForRowAtIndexPath: method to 尝试将cellForRowAtIndexPath:方法更改为

- (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. 由于每一节都有一行, indexPath.row在所有情况下indexPath.row都将zero So only first element of both BranchName and enterPrise_names array will be shown in labels. 因此,标签中只会显示BranchNameenterPrise_names数组的第一个元素。 Changing indexPath.row to indexPath.section will solve the problem indexPath.row更改为indexPath.section将解决问题

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM