2013-07-29 29 views
1

有關更改單元格樣式的問題。 在情節串連圖板,我已經創建了一個細胞:內容:動態原型更改部分的uiTableview單元格樣式

在代碼

,段的數量:2。

第一個可以使用在故事板創建的佈局。這些單元格充滿了來自數據庫的數據。 我試圖將第2節中單元格的佈局更改爲value1樣式。

這怎麼辦?

某些代碼:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    static NSString *CellIdentifier = @"cartCell"; 
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath]; 

    switch (indexPath.section) { 

     case 0: 
     {  
      // Configure the cell... 
      NSInteger row = [indexPath row]; 
      [[cell textLabel] setText:[cartItems objectAtIndex:row]]; 

      return cell; 
      break; 
     } 

     case 1: 
     { 


      NSInteger row = [indexPath row]; 
      switch (row) { 
       case 0: 
        [[cell textLabel] setText:@"Total1"]; 
        break; 
       case 1: 
        [[cell textLabel] setText:@"Total2"]; 
      } 

      return cell; 
      break; 
     } 
    } 
} 
+0

你應該嘗試我們的答案,讓你在忙什麼反饋。 –

+0

將查找答案。給我一些時間,這是一個側面項目。 – TomVD

回答

2

要做到這一點的方法是在故事板中創建2個不同的動態原型,每個原型都有自己的標識符。然後在的cellForRowAtIndexPath,剛剛出列與switch語句,否則,如果子句中你想要的標識符細胞:

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

    if (indexPath.section == 0) { 
     UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"Cell" forIndexPath:indexPath]; 
     cell.textLabel.text = self.theData[indexPath.section][indexPath.row]; 
     return cell; 

    }else{ 
     UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"Cell_value1" forIndexPath:indexPath]; 
     cell.textLabel.text = self.theData[indexPath.section][indexPath.row]; 
     cell.detailTextLabel.text = @"detail"; 
     return cell; 

    } 
} 
+0

在故事板中創建多個原型的技巧。不知道你可以創建多個。 – TomVD

0

您需要設置單元格的單元格樣式 試試下面的代碼

There are four inbuilt styles supported in iOS 
    1. UITableViewCellStyleDefault 
    2. UITableViewCellStyleValue1 
    3. UITableViewCellStyleValue2 
    4. UITableViewCellStyleSubtitle 

嘗試和他們一起玩,讓你期望的結果

靜態NSString * reuseIdentifer = @「ReuseIdentifier」;

// Try to reuse the cell from table view 
     UITableViewCell *cell = 
     [tableView dequeueReusableCellWithIdentifier:reuseIdentifer]; 

     // If there are no reusable cells; create new one 
     if (cell == nil) { 
      cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault 


             reuseIdentifier:reuseIdentifer]; 
      } 
cell.textLabel.tex = @"Your desired text"; 
return cell;