【问题标题】:how to create custom cell with label & textfield in objective c如何在目标c中创建带有标签和文本字段的自定义单元格
【发布时间】:2016-03-08 06:56:25
【问题描述】:

我已经采取了标题让我们说的部分

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

以下是代码和屏幕截图。

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

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

【问题讨论】:

  • 把你的问题解释清楚。
  • 嗨 chathurka,我在表格视图部分添加了按钮,当我单击该按钮时,我想为该特定部分添加表格单元格

标签: ios objective-c uitextfield custom-cell uitableview


【解决方案1】:

你可以这样做,如下所示,

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

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

}

这就是我所做的:

【讨论】:

    【解决方案2】:

    请阅读评论文本。

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-09-08
      • 1970-01-01
      相关资源
      最近更新 更多