2012-02-21 122 views
0

我試圖將值傳遞給表格單元格,但是當我用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; 

} 
+0

如何將userData傳遞給此類? – beryllium 2012-02-21 21:42:55

+0

#導入「UserData.h」,然後UserData * userdata; – user1191343 2012-02-21 21:44:01

+0

這不是一個傳球,它只是一個頭部導入。在哪裏創建它,初始化和設置爲表的數據源? – beryllium 2012-02-21 21:45:30

回答

0

從一個視圖控制器(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]; 

應該有一些調整,但主要部分在這裏。

祝你好運。