如何在 objective c 中创建带有标签和文本字段的自定义单元格

how to create custom cell with label & textfield in objective c

我已经选择了标题为

的部分

星期一、星期二、星期三......星期日 etc.also 我在章节标题后添加了“+”按钮并在该按钮上添加了操作

下面是代码和截图。

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    // Return the number of sections.
    return [SectionArray count];
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    if (section ==0) {
        return 0;
    }else{
            return 3;
    }
}
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{
    return [SectionArray objectAtIndex:section];
}
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
    CGRect frame = tableView.frame;
    UIView *headerView;
    if(section == 0 || section == 7) {

    }else{
    UIButton *addButton=[UIButton buttonWithType:UIButtonTypeContactAdd];
    addButton.frame =CGRectMake(frame.size.width-60, 5, 50,30);
    addButton.titleLabel.text = @"+";
        addButton.tag =section;
   // addButton.backgroundColor = [UIColor grayColor];
        [addButton addTarget:self action:@selector(AddTimeSlot:) forControlEvents:UIControlEventTouchUpInside];
    UILabel *title = [[UILabel alloc] initWithFrame:CGRectMake(30, 10, 100, 30)];
    title.text = [SectionArray objectAtIndex:section];

    headerView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, frame.size.width, frame.size.height)];
    [headerView addSubview:title];
    [headerView addSubview:addButton];
    }
    return headerView;

}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{

    NSString *cellIdent = @"cell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdent];

    if(cell == nil){

        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdent];
    }
    return cell;
}

现在我必须在单击“+”按钮时创建单元格,所以请帮助我。

请阅读评论文字。

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{

  /** should be change in dynamically
    maintain array for every section rows
    return row count accordint to section
   **/
}

#pragma mark - Private methods

- (void)AddTimeSlot:(UIButton *)sender {

    int sectionNo = sender.tag;
    // Get you section rowArray(DATASOURCE) and insert you detaisl
    // then add UI

    NSIndexPath *indexPath = [NSIndexPath indexPathForRow:0 inSection:0];

    [tableView beginUpdates];
    [tableView insertRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationNone];
    [tableView endUpdates];
}

您可以按照下面的方式进行,

首先,您必须使用数组作为动态操作的数据源。

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    if (section ==0) {
        return 0;
    }else{
        return numberOfRowNeedToDisplay; //Take global int variable
    }
}

在您的按钮操作中添加时间段:

-(void)addTimeSlot:(id)sender {

//If you want to add cell to section
NSMutableArray *arrayIndexPathsToBeAdded = [[NSMutableArray alloc] init];

    for (int i = 0; i < <numberOfCellsYouWantToAdd>; i++) {

       [arrayIndexPathsToBeAdded addObject:[NSIndexPath indexPathForRow:i inSection:view.tag]];

    }

    numberOfRowNeedToDisplay = <numberOfCellsYouWantToAdd>;

    [self.tableView beginUpdates];
    [self.tableView insertRowsAtIndexPaths:arrayIndexPathsToBeAdded withRowAnimation:UITableViewRowAnimationRight];

    [self.tableView endUpdates];

//If you want to remove cells
NSMutableArray *arrayIndexPathsToBeRemoved = [[NSMutableArray alloc] init];

    for (int i = 0; i < <numberOfCellsYouWantToRemove>; i++) {
        [arrayIndexPathsToBeRemoved addObject:[NSIndexPath indexPathForRow:i inSection:sectionIndexToBeExpanded]];
    }

    numberOfRowNeedToDisplay = <numberOfCellsYouWantToRemove>;

    [self.tableView beginUpdates];
    [self.tableView deleteRowsAtIndexPaths:arrayIndexPathsToBeRemoved withRowAnimation:UITableViewRowAnimationLeft];

}

这是我所做的: