某种文本字段 - 表视图

时间:2013-03-28 10:55:13

标签: ios objective-c

enter image description here

我想知道如何获得上面的图像。你怎么做到这一点,有没有人得到某种示例代码?

3 个答案:

答案 0 :(得分:1)

您需要为桌子创建自定义单元格,根据需要设计单元格。放置textView或任何东西,然后使用该自定义单元格加载表。

答案 1 :(得分:0)

它被称为自定义UITableViewCell,包含两个控件

1 UILabel 
2 UITextField 

将这两个控件放在具有特定fram大小的cellForRowAtIndexPath中。

按原样放置整个代码。

- (void)viewDidLoad
{
    [super viewDidLoad];

    self.tblView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, self.view.bounds.size.width, self.view.bounds.size.height - 23) style:UITableViewStyleGrouped];
    self.tblView.delegate = self;
    self.tblView.dataSource = self;
    self.tblView.backgroundView = nil;
    self.tblView.backgroundColor = [UIColor clearColor];
    self.tblView.separatorStyle = UITableViewCellSeparatorStyleNone;
    [self.view addSubview:self.tblView];

    self.placeHolderValue = [[NSMutableArray alloc]initWithObjects:@"server ", @"account", @"Roof Coverings", @"Wall token", @"Roof", @"Floor", @"Guttering",  nil];


    self.textFields = [NSMutableArray array];
    self.textLabels = [NSMutableArray array];

    for (int i = 0; i < self.placeHolderValue.count; i++)
    {
        UITextField *textFiled = [[UITextField alloc] initWithFrame:CGRectMake(155.0, 0.0, 150.0, 44.0)];
        textFiled.placeholder = @"Vereist";
        textFiled.contentVerticalAlignment = UIControlContentVerticalAlignmentCenter;
        textFiled.autocorrectionType = UITextAutocorrectionTypeNo;
        textFiled.autocapitalizationType = UITextAutocapitalizationTypeNone;
        textFiled.borderStyle = UITextBorderStyleNone;
        textFiled.clearButtonMode = UITextFieldViewModeAlways;
        textFiled.delegate = self;
        textFiled.tag = i+1;
        textFiled.font = [UIFont systemFontOfSize:14];
        if (self.listOfDetails.count > 0)
        {
            textFiled.text = [[self.listOfDetails objectAtIndex:0] objectForKey:[self.dbFieldName objectAtIndex:i]];
            isEmpty = NO;
        }
        [self.textFields addObject:textFiled];

        UILabel *lab = [[UILabel alloc]initWithFrame:CGRectMake(15, 0, 140, 44)];
        lab.text = [self.placeHolderValue objectAtIndex:i];
        lab.textAlignment = NSTextAlignmentLeft;
        lab.numberOfLines = 0;
        lab.font = [UIFont systemFontOfSize:14];
        lab.backgroundColor = [UIColor clearColor];
        [self.textLabels addObject:lab];
    }
}

#pragma mark -
#pragma mark Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    // Return the number of sections.
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    // Return the number of rows in the section.
    return self.placeHolderValue.count;
}

// Customize the appearance of table view cells.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

    //static NSString *CellIdentifier = @"Cell";
    NSString *CellIdentifier = [NSString stringWithFormat:@"S%1dR%1d",indexPath.section,indexPath.row];

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {

        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        [cell.contentView addSubview:[self.textLabels objectAtIndex:indexPath.row]];
        [cell.contentView addSubview:[self.textFields objectAtIndex:indexPath.row]];
    }
    return cell;
}

答案 2 :(得分:0)

设计您的自定义单元格(如果需要,请使用标签的自定义字体以获得精确的外观。)

自定义单元格设计

enter image description here

并按索引委托方法自定义Cell for row。

 - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
    {
    //    static NSString *CellIdentifier = @"Cell";
    //    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    //    if (cell == nil) {
    //        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
    //    }
    //    
        // Configure the cell...

        //Customiztion of  cell 
        static NSString *cellIdentifier=@"cell";
        SampleTableCell *cell = (SampleTableCell *) [tableView dequeueReusableCellWithIdentifier:cellIdentifier];

        if (cell == nil)
        {
            NSString *customeCellName = [NSString stringWithFormat:@"%@",[SampleTableCell class]];

            NSArray *topLevelObjects = [[NSBundle mainBundle] loadNibNamed:customeCellName owner:self options:nil];

            for (id currentObject in topLevelObjects)
            {
                if ([currentObject isKindOfClass:[UITableViewCell class]])
                {
                    cell =  (SampleTableCell *) currentObject;
                    break;
                }
            }
        }
        cell.label1.text = @"Server";
        cell.label2.text = @"vereist"
        return cell;
    }