2012-02-23 66 views
0

我传递一个名为userArray的数组,其中存储了另一个带有字符串的数组。这是我迄今为止的,但我知道这是错误的。有人能指引我朝着正确的方向吗?从数组中添加单元格到UITableView

-(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; 
    } 
} 
+0

是您的收益咋办在for循环中? – ferdil 2012-02-23 05:53:37

回答

1

cellForRowAtIndexPathUITableViewDataSource方法,并且它要求仅单个单元中的数据。所以,你必须删除一个循环,并成立了一个单细胞一次,用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]

+0

您能否通过返回计数来进一步解释您的意思?编辑:NVM我看到你的意思 – mkral 2012-02-23 06:01:57

+0

您的意思是:'返回[[[DataSingleton sharedData] usersArray] count];' ?无论哪种方式我得到一个空值 – mkral 2012-02-23 06:18:08

+1

是的,编辑帖子。 – anticyclope 2012-02-23 08:58:59