Ios 尝试使用nsmutablearray呈现uitableview
好的,我尽力解释我的问题,我试着在用户从他们的设备中挑选完一张图片后创建一个数组Ios 尝试使用nsmutablearray呈现uitableview,ios,objective-c,uitableview,nsmutablearray,Ios,Objective C,Uitableview,Nsmutablearray,好的,我尽力解释我的问题,我试着在用户从他们的设备中挑选完一张图片后创建一个数组 - (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info { listReceived = [NSArray arrayWithObjects:labelName.text,labelAddres
- (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。但在搜索中仍然没有运气。
如果你们能帮助我,我真的很感激
谢谢请尝试此代码,它可能会对您有所帮助
- (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"];
如果您在尝试此代码后遇到任何问题,请告诉我。您有什么问题?它不起作用了?您是否实现了另一个表的数据源和委托方法?您好,sagarcool89,由于未捕获的异常“NSInvalidArgumentException”,错误正在终止应用程序,原因:'-[\u NSArrayI isEqualToString:::]:未识别的选择器已发送到指向cell.Label1.text=textStr的实例0xa0cdb20'。此行错误--NSString*textStr=[allListReceived objectAtIndex:indexPath.row];您的allListReceived在其索引处包含数组。若要检索标签文本,您需要进一步:NSString*textStr=[[allListReceived objectAtIndex:indexPath.row]objectAtIndex:0];并以同样的方式检索所有信息。我建议您将信息存储在NSMutableDictionary中,并将其添加到NSMutableArray中。并使用NSMutableDictionary将信息显示到tableview中。您好sagarcool89和Puneet,这是我根据在stackoverflow中的搜索尝试做的,但我找不到任何与我的问题相关的文章,如果你们能给我发送链接让我进行研究,我将不胜感激。谢谢。检查我编辑的答案,如何使用NSMutableDictionary而不是NSString。