2012-08-22 42 views
0

我正在开发一个iOS应用程序,有数据作为字符串我从服务器使用RestKit检索。数据检索成功,我可以使用NSlog打印它。所以我想在下面的tableviewcell中查看这些数据。填充自定义tableviewcell Xcode

enter image description here

所以如何实现tableviewcell通过,我使用RestKit从服务器检索设备,描述和日期的列表来填充。

在此先感谢。

+0

第一个问题,你如何实现该TableView?使用Nib文件?故事情节?只通过代码?首先回答这个问题,将有助于告诉你下一步你需要做什么。 –

回答

0

这很容易,但需要一点时间。您需要将后端数据放入数组(或类似的东西)中 - 在扩展UITableViewController的类中,然后将UILabels作为插座连接起来,并且在函数中,您可以将数据分配给特定索引处的单元格。

cell.deviceDescription.text = [myDataArray objectAtIndex:[indexPath row];

0

您设置单元格的高度

tableView.rowHeight=height_you_want; 
     or 
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    return your_row_height; 
} 

的cellForRowAtIndexPath:您可以创建了三个的UILabel,修改(背景文本字体颜色大小等),并将它们作为子视图添加到细胞或cell.contentView

UILabel *lbl=[[UILabel alloc]initWithFrame:CGRectMake(<#CGFloat x#>, <#CGFloat y#>, <#CGFloat width#>, <#CGFloat height#>)];  
[email protected]"Your text"; 
lbl.backgroundColor=[UIColor clearColor];  
[cell addSubview:lbl]; 

//use this accessory type  
cell.accessoryType=UITableViewCellAccessoryDisclosureIndicator; 
+0

我已经使用接口生成器XIB创建了单元,但是我想从NSMutableArray中读取并填充单元格。 – fadd

+0

你的意思是你使用接口生成器xib创建了一个单元。你的问题是如何以编程方式从nsmutablearray填充标签? – Neo

+0

是的,这就是我想要的。 – fadd

0

用xib创建您的自定义单元格。

@interface YourCustomCell : UITableViewCell 
@property(nonatomic, strong) IBOutlet UILabel* deviceNameLbl; 
@property(nonatomic, strong) IBOutlet UILabel* desciptionLbl; 
@property(nonatomic, strong) IBOutlet UILabel* dateLbl; 
@end 

@implementation 
@synthesize deviceNameLbl ,....... 
...... 

enter image description here 然后在您的视图控制器要显示该单元格。

-(UITableViewCell*)tableView:(UITableView*)tableView cellForRowAtIndexPath:(NSIndexPath*)indexPath { 
    static NSString* cellIdentifier = @"YourCustomCell"; 
    YourCustomCell *cell = (YourCustomCell*)[tableView dequeueReusableCellWithIdentifier:cellType]; 
    if (cell == nil) { 
    NSArray *nib = [[NSBundle mainBundle] loadNibNamed:cellIdentifier owner:nil options:nil]; 
    cell = (YourCustomCell*)[nib objectAtIndex:0]; 
     } 

    //yourDataArray is a array contains NSDictionary 
    cell.deviceNameLbl = [[yourDataArray objectAtIndex:indexPath.row][email protected]"deviceName"]; 
    cell.desciptionLbl = [[yourDataArray objectAtIndex:indexPath.row][email protected]"description"]; 
    cell.dateLbl = [[yourDataArray objectAtIndex:indexPath.row][email protected]"date"]; 

    return cell; 
} 
+0

如果您已经在使用Interface Builder构建tableview,则无需使用单独的nib文件创建单元格。只需创建UITableViewCell子类并将UITableView Cell的父类设置为该类。 –

0
  1. 您需要通过数据阵列中创建自定义UITableViewCell
  2. 循环您UITableViewController
  3. - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath必须分配/初始化类

但作为@FaddishWorm说的它会需要一些时间