我试图将值传递给表格单元格,但是当我用 userdata.title 替换字符串时 它显示为空白。 userdata 是另一个类 UserData 的对象,它是外部的,我导入了它。从所有 View 访问 UserData 以将对象从一个 Controller 传递到另一个 Controller 。现在,我如何将值传递给表,如果我决定通过我的文本字段添加更多值,我该如何保留它?
- (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;
}
最佳答案
要将数据从一个 ViewController(例如 vc1)传递到另一个 ViewController(vc2),您可以为目标 Controller 设置一个属性。所以对于上面的 UIViewController,你应该在 .h 中添加
@property (nonatomic, retain) UserData *tableDatas;
然后,在 View Controller vc1 中,您将拥有:
vc2.tableDatas = userdatas;
最后,在 UITableViewDelegate 方法中:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
你可以使用:
cell.textLabel.text = [self.tableDatas objectAtIndex:indexPath.row];
应该有一些调整,但主要部分在这里。
祝你好运。
关于ios - 将数据传递到表格单元格,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9385493/