将数据传递到表格单元格

时间:2012-02-21 21:32:20

标签: ios objective-c uitableview

我正在尝试将值传递给表格单元格但是当我用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;

}

1 个答案:

答案 0 :(得分:0)

要将数据从一个ViewController(例如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];

应该有一些调整,但主要部分在这里。

祝你好运。