【发布时间】:2014-12-10 06:38:05
【问题描述】:
我是 iOS 新手。我正在制作一个应用程序,在该应用程序中我得到了来自 json 的响应,一切正常。我将响应存储在一个数组中,但是当我像这样将数组值设置为 tableview 单元格时
valuedate.text=[[self.inboxmessagesarray objectAtIndex:indexPath.row]objectForKey:@"messageId"]
这是我所误解的一个例外。提前感谢。下面是我的示例代码。
-(void)viewdidload{
NSMutableDictionary *callDict =[[NSMutableDictionary alloc] init];
[callDict setObject:@"messages-getModuleMessages" forKey:@"call"];
[callDict setObject:FB_API_KEY forKey:@"accessSecret"];
NSString *x=[FBUserManager sharedUserManager].authToken;
[callDict setObject:x forKey:@"authToken"];
[callDict setObject:@"json" forKey:@"format"];
[callDict setObject:@"inbox" forKey:@"callType"];
FBGenericWebHandler *handler = [[FBGenericWebHandler alloc] init];
handler.delegate = self;
[handler Inboxmessages:callDict];
handler = nil;
[MBProgressHUD showHUDAddedTo:self.view animated:YES];
}
我得到这样的回应
-(void)inboxmessagesGetSuccess:(FBGenericWebHandler*)handler response:(NSDictionary*)response
{
[MBProgressHUD hideHUDForView:self.view animated:YES];
NSMutableArray *inboxArray = [[NSMutableArray alloc] init];
NSArray *firstarray=[[[response objectForKey:@"messageResponse"] objectAtIndex:1] objectForKey:@"messages"];
for(NSDictionary *tmp in firstarray)
{
NSMutableDictionary *messages=[[NSMutableDictionary alloc]init];
[messages setValue:[tmp objectForKey:@"messageId"] forKey:@"messageId"];
[self.inboxmessagesarray addObject:messages];
}
[self.activitiesTableView_ reloadData];
}
并像这样设置我的单元格
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
static NSString *tableviewidentifier = @"cell";
tablecellTableViewCell *cell= [self.activitiesTableView_ dequeueReusableCellWithIdentifier:tableviewidentifier];
if(cell==nil)
{
cell = [[tablecellTableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:tableviewidentifier];
}if(indexPath.row == [self tableView:tableView numberOfRowsInSection:indexPath.section] - 1){
// [[cell textLabel] setText:@"Load more records"];
}
UILabel *valuedate = (UILabel *)[cell viewWithTag:21];
UILabel *msg = (UILabel *)[cell viewWithTag:22];
UILabel *date = (UILabel *)[cell viewWithTag:23];
UILabel *time = (UILabel *)[cell viewWithTag:24];
valuedate.text=[[self.inboxmessagesarray objectAtIndex:indexPath.row]objectForKey:@"messageId"];
return cell;
}
【问题讨论】:
-
你的
numberOfRowsInSection:方法是什么? -
另外,您的单元格是否定义为情节提要中的原型单元格?如果是这样,您可以跳过整个
if (cell==nil...位。另外,我建议创建一个 UITableViewCell 子类并使用IBOutlet属性,而不是搜索带有标签的视图。最后,使用传递给方法的tableView比使用self.activitiesTableView_更简洁(并从属性中去掉尾随的_)
标签: ios arrays uitableview