2015-10-16 49 views
1

我試着添加一個按鈕到一個新的細胞在tableview中,例如,我需要在小區「1」按鈕,如果暗淡不爲0UITableView中添加新的單元格按鈕

但是我不能去做吧。請賜教。

- (void)viewDidLoad 
    { 
     [super viewDidLoad]; 
     _displaynum = [NSArray arrayWithObjects:@"1",@"2",@"3", nil]; 
     _displayID = [NSArray arrayWithObjects:@"ID = 1",@"ID = 2",@"ID = 3", nil]; 
    } 

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

     return [_displaynum count]; 

    } 

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

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


     if (cell == nil){ 
      cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:lightIdentifier]; 
     } 
     UISwitch *switchLights = [[UISwitch alloc] initWithFrame:CGRectMake(1.0, 1.0, 20.0, 20.0)]; 
     cell.textLabel.text = [_displayLights objectAtIndex:indexPath.row]; 
     cell.detailTextLabel.text =[_displayLightsID objectAtIndex:indexPath.row]; 
     cell.accessoryView = switchLights; 
     switchLights.on = NO; 
     int dim = 1; 
     if ((dim =! 0)) { 
      NSArray *insertIndexPaths = [NSArray arrayWithObjects: 
           [NSIndexPath indexPathForRow:1 inSection:0], 
           nil]; 
      NSArray *deleteIndexPaths = [NSArray arrayWithObjects: 
           [NSIndexPath indexPathForRow:2 inSection:0],nil]; 

      [tableView beginUpdates]; 
      [tableView insertRowsAtIndexPaths:insertIndexPaths withRowAnimation:UITableViewRowAnimationRight]; 
      [tableView deleteRowsAtIndexPaths:deleteIndexPaths withRowAnimation:UITableViewRowAnimationFade]; 
      [tableView endUpdates]; 
     } 

     return cell; 
    } 



    } 

回答

2

首先,cellForRowAtIndexPath:不適合以您正在操作的方式插入或刪除行。

cellForRowAtIndexPath:每當新的單元格被iOS繪製時被調用。所以,這是您可以修改單元格並簡單地返回它的地方。

這就是說,這是你需要做的:

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

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


    if (cell == nil) { 
     cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:lightIdentifier]; 
    } 

    int dim = 1; 

    if (dim != 0 && indexPath.row == 1) { 
     CGRect buttonRect = CGRectMake(210, 25, 65, 25); 
     UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect]; 
     button.frame = buttonRect; 
     // set the button title here if it will always be the same 
     [button setTitle:@"Action" forState:UIControlStateNormal]; 
     button.tag = 1; 
     [button addTarget:self action:@selector(myAction:) forControlEvents:UIControlEventTouchUpInside]; 
     [cell.contentView addSubview:button]; 
    } 

    return cell; 
} 
+0

感謝上的tableView的解釋,我已經逐漸接近的結果。但按鈕似乎與第二個單元覆蓋。 – aka

+0

您需要根據您的細胞高度調整按鈕框架。你也可以增加細胞高度。讓我知道你是否需要任何幫助。 – Abhinav

+0

我對按鈕框架有所瞭解,但按鈕與第二個對象疊加確切。對於exp,單元格1(對象1),按鈕,單元格2(對象2)。在此先感謝 – aka

相關問題