0

好的,我尽力解释我的问题,我尝试在用户从他们的设备中挑选图片后创建一个数组

- (void)imagePickerController:(UIImagePickerController *)picker
        didFinishPickingMediaWithInfo:(NSDictionary *)info
        {

        listReceived = [NSArray arrayWithObjects:labelName.text,labelAddress.text,labelAge.text,@"filePicname.png",nil];
       }

它可以重复多次(不止一次),所以我尝试把它放在 nsmutablearray

[allListReceived addObject:listReceived];

之后,我想通过使用自定义单元格在 uitableview 中显示所有内容。这是我尝试做的:

 - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
     static NSString *CellIdentifier = @"TopCell";

        MyCustomCell *cell = (MyCustomCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];


        if (cell == nil) 
        {
            NSArray *topLevelObjects;
            if(UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPhone)
            {
                //topLevelObjects = [[NSBundle mainBundle]loadNibNamed:@"MyCustomCell-iPhone.xib" owner:nil options:nil];
            }else{
                topLevelObjects = [[NSBundle mainBundle]loadNibNamed:@"MyCustomCell-iPad.xib" owner:nil options:nil];
        }
            for (id currentObject in topLevelObjects)
            {
                if ([currentObject isKindOfClass:[UITableViewCell class]])
                {
                    cell = (MyCustomCell *) currentObject;
                    cell.Label1.text = [allListTransfer objectAtIndex:indexPath.row];
                    cell.Label2.text = [allListTransfer objectAtIndex:indexPath.row];
                    cell.Label3.text = [allListTransfer objectAtIndex:indexPath.row];
                    cell.Label4.text = [allListTransfer objectAtIndex:indexPath.row];
                    cell.Label5.text = [allListTransfer objectAtIndex:indexPath.row];
                    break;

                }
            }
        }


        return cell;

    }

是的,我知道 nsmutablearray 尚未使用,并且自定义单元格中的所有标签都是与数组第一个索引相同的字符串,我读到我必须使用 nsdictionary。但仍然没有运气。如果你们能帮助我,我真的很感激。

谢谢

4

1 回答 1

1

请尝试此代码,它可能会对您有所帮助。

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

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    // Return the number of rows in the section.
    return [allListReceived count];
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return 44.0;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"TopCell";
        MyCustomCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
        if (cell == nil) {
            NSArray* views = [[NSBundle mainBundle]   loadNibNamed:@"MyCustomCell-iPad" owner:nil options:nil];
            for (UIView *view in views) {
                if([view isKindOfClass:[UITableViewCell class]])
                {
                    cell = (MyCustomCell*)view;
                }
            }
        }

        NSString *textStr = [allListReceived objectAtIndex:indexPath.row];

        cell.Label1.text = textStr;
        cell.Label2.text = textStr;
        cell.Label3.text = textStr;
        cell.Label4.text = textStr;
        cell.Label5.text = textStr;

        return cell;

}

如果您存储 NSMutableDictionary ,请遵循以下代码:

/* Add Objects To Array */
- (void)imagePickerController:(UIImagePickerController *)picker
        didFinishPickingMediaWithInfo:(NSDictionary *)info
 {
      NSMutableDictionary *dictInfo = [[[NSMutableDictionary alloc] init] autorelease];
     [dictInfo setValue:labelName.text forKey:@"label1"];
     [dictInfo setValue:labelAddress.text forKey:@"label2"];
     [dictInfo setValue:labelAge.text forKey:@"label3"];
     [dictInfo setValue:@"filePicname.png" forKey:@"label4"];

     [allListReceived addObject:dictInfo];
 }


/* Display in UITableView */
NSMutableDictionary *dictInfo = [allListReceived objectAtIndex:indexPath.row];

cell.Label1.text = [dictInfo valueForKey:@"label1"];
cell.Label2.text = [dictInfo valueForKey:@"label2"];
cell.Label3.text = [dictInfo valueForKey:@"label3"];
cell.Label4.text = [dictInfo valueForKey:@"label4"];
cell.Label5.text = [dictInfo valueForKey:@"label5"];

如果您在尝试此代码后遇到任何问题,请告诉我。

于 2013-07-16T10:40:02.543 回答