2016-03-08 33 views
3

我已經採取了部分跟標題讓說如何創建自定義單元格與目標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; 
} 

this is i have made format of design

現在我要創建當我點擊「+」按鈕,電池,所以請幫助我。

+0

解釋清楚你的問題。 – Chathurka

+0

嗨chathurka, 我已經添加在表視圖部分按鈕和我那個按鈕我要添加表格單元格爲特定部分 –

回答

4

你可以做如下,

首先,你必須使用的,將被動態操縱的數據源的數組。

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

+0

嗨巴拉特, 感謝您的回覆點擊.....它的工作原理。 –

+1

您應該將答案標記爲已接受,因爲它的工作原理@PoojaBorkar –

0

請閱讀評論文字。

- (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]; 
} 
相關問題