填充自定义tableviewcell Xcode

时间:2012-08-22 11:09:42

标签: objective-c

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

enter image description here

那么如何使用RestKit从服务器检索的设备列表,描述和日期来实现tableviewcell。

提前致谢。

4 个答案:

答案 0 :(得分:0)

这很容易,但需要一点时间。您需要将后端数据放入数组(或类似的东西) - 在扩展UITableViewController的类中,然后将UILabel作为出口连接,在tableView:cellForRowAtIndexPath函数中,您可以将数据分配给单元格一个特定的指数。

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

答案 1 :(得分: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#>)];    
lbl.text=@"Your text";
lbl.backgroundColor=[UIColor clearColor];    
[cell addSubview:lbl];

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

答案 2 :(得分: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]objectForKey@"deviceName"];
   cell.desciptionLbl = [[yourDataArray objectAtIndex:indexPath.row]objectForKey@"description"];
   cell.dateLbl = [[yourDataArray objectAtIndex:indexPath.row]objectForKey@"date"];

   return cell;
}

答案 3 :(得分:0)

  1. 您需要创建自定义UITableViewCell
  2. UITableViewController
  3. 中循环浏览数据数组
  4. - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath中,您必须分配/初始化您的课程
  5. 但正如@FaddishWorm说的那样需要一些时间

相关问题