ios - 如何在 Objective C 中创建带有标签和文本字段的自定义单元格

标签 ios objective-c uitextfield custom-cell uitableview

我已经采取了标题部分

星期一、星期二、星期三......星期日等。我还在部分标题后添加了“+”按钮并在该按钮上添加了操作

下面是代码和屏幕截图。

- (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;
}

this is i have made format of design

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

最佳答案

您可以按如下方式执行此操作,

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

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

在按钮操作 AddTimeSlot 中:

-(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];

}

这就是我所做的:

enter image description here

关于ios - 如何在 Objective C 中创建带有标签和文本字段的自定义单元格,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35860929/

相关文章:

ios - swift 3 google maps - 如何更新 map 上的标记

iphone - 将 NSString 绘制到 PDF 页面上

ios - 使用 2 个 UITextFields 进行密码确认

iOS应用程序-可以调试,但不能发布

ios - 带有 Firebase 数据库或 Firestore 读/写的 Swift 4

ios - 在 Xcode/iOS 中使用新项目替换旧应用程序

uiscrollview - 多个文本字段的 ScrollView 不起作用

ios - -[UITableView layoutSublayersOfLayer :] on iOS 7 断言失败

ios - 从 imagePickerController AFNetWorking 3.0 将图像上传到服务器

ios - 使用 UITextView 的子类和自定义屏幕键盘,调用 textField.textShouldChange