Добавление ячеек в UITableView из массива - PullRequest
0 голосов
/ 23 февраля 2012

Я передаю массив с именем userArray, в котором хранится другой массив со строками.Это то, что у меня есть, но я знаю, что это неправильно.Может ли кто-нибудь указать мне правильное направление?

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

    static NSString *CellIdentifier = @"Cell";    
    CustomCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if(cell == nil) {
        cell = [[CustomCell alloc] initWithFrame:CGRectZero reuseIdentifier:CellIdentifier];
    }

    //Set Up Cell
    DataSingleton *sharedData = [DataSingleton sharedData];

    for (NSArray *array in sharedData.usersArray){
        cell.primaryLabel.text = [array objectAtIndex:1];
        cell.secondaryLabel.text = [array objectAtIndex:2];
        cell.profileImage = [UIImage imageNamed:@"111-user.png"];
        return cell;
    }
}

1 Ответ

1 голос
/ 23 февраля 2012

cellForRowAtIndexPath - это метод UITableViewDataSource, который запрашивает данные только для одной ячейки.Таким образом, вы должны удалить цикл и настроить одну ячейку сразу, используя indexPath.row в качестве индекса массива в вашем DataSingleton

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

    static NSString *CellIdentifier = @"Cell";
    CustomCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if(cell == nil) {
        cell = [[CustomCell alloc] initWithFrame:CGRectZero reuseIdentifier:CellIdentifier];
    }

    //Set Up Cell
    DataSingleton *sharedData = [DataSingleton sharedData];

    NSArray *array = [sharedData.usersArray objectAtIndex:indexPath.row];
    cell.primaryLabel.text = [array objectAtIndex:1];
    cell.secondaryLabel.text = [array objectAtIndex:2];
    cell.profileImage = [UIImage imageNamed:@"111-user.png"];
    return cell;   
}

Также вы должны реализовать tableView:numberOfRowsInSection: для возврата[[[DataSingleton sharedData] usersArray] count]

...