我正在开发一个iOS应用程序,有数据作为字符串我从服务器使用RestKit检索。数据检索成功,我可以使用NSlog打印它。所以我想在下面的tableviewcell中查看这些数据。填充自定义tableviewcell Xcode
所以如何实现tableviewcell通过,我使用RestKit从服务器检索设备,描述和日期的列表来填充。
在此先感谢。
我正在开发一个iOS应用程序,有数据作为字符串我从服务器使用RestKit检索。数据检索成功,我可以使用NSlog打印它。所以我想在下面的tableviewcell中查看这些数据。填充自定义tableviewcell Xcode
所以如何实现tableviewcell通过,我使用RestKit从服务器检索设备,描述和日期的列表来填充。
在此先感谢。
这很容易,但需要一点时间。您需要将后端数据放入数组(或类似的东西)中 - 在扩展UITableViewController的类中,然后将UILabels作为插座连接起来,并且在函数中,您可以将数据分配给特定索引处的单元格。
cell.deviceDescription.text = [myDataArray objectAtIndex:[indexPath row];
您设置单元格的高度
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;
用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 ,.......
......
然后在您的视图控制器要显示该单元格。
-(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;
}
如果您已经在使用Interface Builder构建tableview,则无需使用单独的nib文件创建单元格。只需创建UITableViewCell子类并将UITableView Cell的父类设置为该类。 –
UITableViewCell
UITableViewController
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
必须分配/初始化类但作为@FaddishWorm说的它会需要一些时间
第一个问题,你如何实现该TableView?使用Nib文件?故事情节?只通过代码?首先回答这个问题,将有助于告诉你下一步你需要做什么。 –