将数据传递到表单元格
问题描述:
我试图将值传递给表格单元格,但是当我用userdata.title 替换字符串时,它显示为空白。 userdata是另一个类UserData的对象,它是外部的,我将它导入。 UserData从所有视图访问,以将对象从一个控制器传递到另一个控制器。现在,如何将值传递给表格,并且如果我决定通过文本字段添加更多值,我该如何保留它?将数据传递到表单元格
- (void)viewDidLoad
{
tabledata = [[NSMutableArray alloc] initWithObjects:@"hello", nil];
tablesubtitles = [[NSMutableArray alloc] initWithObjects:@"hello", nil];
[super viewDidLoad];
//if I use "userdata.title" in replacemnent of @"hello" above, the table show up blank
//self.navigationItem.rightBarButtonItem = self.editButtonItem;
}
//--------------------------------------
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section;
{
return [tabledata count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath;
{
UITableViewCell *cell = nil;
cell = [tableView dequeueReusableCellWithIdentifier:@"homeworkcell"];
if(cell == nil)
{
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:@"homeworkcell"];
}
cell.textLabel.text = [tabledata objectAtIndex:indexPath.row];
cell.detailTextLabel.text = [tablesubtitles objectAtIndex:indexPath.row];
cell.textLabel.font = [UIFont systemFontOfSize:14.0];
//static NSString *CellIdentifier = @"Cell";
/* if (cell == nil) {
cell = [[UITableViewCell alloc]
initWithStyle:UITableViewCellStyleSubtitle
reuseIdentifier:CellIdentifier];
}*/
// Configure the cell.
//-----------------------------------------START----------------------------Set image of cell----
cellImage = [UIImage imageNamed:@"checkboxblank.png"];
cell.imageView.image = cellImage;
//--------------------------------------------END---------------------------end set image of cell--
return cell;
}
答
从一个视图控制器(VC1例如)数据传递到另一个(VC2),你可以设置一个属性它的目标控制器。因此,对于上述的UIViewController,你应该在.H添加
@property (nonatomic, retain) UserData *tableDatas;
然后,在视图控制器VC1,你将有:
vc2.tableDatas = userdatas;
最后,在方法的UITableViewDelegate:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
你可以使用:
cell.textLabel.text = [self.tableDatas objectAtIndex:indexPath.row];
应该有一些调整,但主要部分在这里。
祝你好运。
如何将userData传递给此类? – beryllium 2012-02-21 21:42:55
#导入“UserData.h”,然后UserData * userdata; – user1191343 2012-02-21 21:44:01
这不是一个传球,它只是一个头部导入。在哪里创建它,初始化和设置为表的数据源? – beryllium 2012-02-21 21:45:30