2015-03-25 71 views
0

我正在開發一個帶有tableview的iPhone應用程序,充當不同部分的窗體。我也有3個自定義單元格,其中包含文本字段,段控制和UISwitchView。我的問題是,當我按下開關或在textField之一中鍵入一些文本時,Switch的相同狀態或文本字段的文本重新出現在不同的行和不同的部分。UITableView與customcell

下面是一些圖片和我的代碼部分來形容我的問題:

enter image description here enter image description here enter image description here

-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView 
{ 
    return 3; 
} 

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section 
{ 
    NSInteger rows = 0; 
    switch (section) { 
     case 0: 
      rows = 5; 
      break; 
     case 1: 
      rows = 11; 
      break; 
     case 2: 
      rows = 9; 
      break; 
     default: 
      break; 
    } 

    return rows;  
} 


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


    // init cell 
    switch (indexPath.section) { 
     case 0: 
      if (indexPath.row>=0 && indexPath.row<=3) 
      { 

       cell = [tableView dequeueReusableCellWithIdentifier:@"text_field_cell"]; 

      } 
      else if (indexPath.row == 4) 
      { 
       cell = [tableView dequeueReusableCellWithIdentifier:@"segment_cell"]; 

      } 
      break; 
     case 1: 
      if (indexPath.row >=0 && indexPath.row <= 9) 
      { 
       cell = [tableView dequeueReusableCellWithIdentifier:@"switch_cell"]; 

      } 
      else if (indexPath.row == 10) 
      { 
       cell = [tableView dequeueReusableCellWithIdentifier:@"text_field_cell"]; 

      } 
      break; 
     case 2: 
      if (indexPath.row >=0 && indexPath.row <= 6) 
      { 
       cell = [tableView dequeueReusableCellWithIdentifier:@"switch_cell"]; 

      } 
      else if (indexPath.row >=7 && indexPath.row <=8) 
      { 
       cell = [tableView dequeueReusableCellWithIdentifier:@"text_field_cell"]; 

      } 
      break; 

     default: 
      break; 
    } 

// init all text .. 

return cell; 

} 
+0

如何創建自定義單元格?發佈您的單元分配代碼。 – Nirmalsinh 2015-03-25 10:41:04

+0

我正在故事板中創建原型單元格,之後: cell = [tableView dequeueReusableCellWithIdentifier:@「text_field_cell」]; UILabel * label =(UILabel *)[cell viewWithTag:1]; UITextField * textField =(UITextField *)[cell viewWithTag:2]; UISwitch * uiswitch =(UISwitch *)[cell viewWithTag:2]; UISegmentedControl * segmentControl =(UISegmentedControl *)[cell viewWithTag:2]; – user3414609 2015-03-25 11:00:01

回答

0

我會實現-tableView:willDisplayCell:forRowAtIndexPath:到你的細胞設置爲每行一個正確的狀態。