2012-12-19 32 views
7

我想在我的tableview中顯示來自web的不同數據,但我沒有得到如何在單獨的單元格中顯示它們的一個表格部分,任何人都可以幫助我顯示 一個小區如何在tableView中添加5個不同的自定義單元格

cell.textLabel.text=app.i_name; 

在第二小區

cell.textLabel.text=app.i_phone; 

在第三小區

cell.textLabel.text=app.i_hours; 

在第五單元第四小區

cell.textLabel.text=app.i_address; 

cell.textLabel.text=app.i_email; 

索引我的行單元格是

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

    static NSString *CellIdentifier = @"Single Cell"; 
    SingleCell *cell =(SingleCell *) [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 


    if (cell == nil) { 
     UIViewController *c = [[UIViewController alloc] initWithNibName:@"SingleCell" bundle:nil]; 
     cell = (SingleCell *) c.view; 
     //[c release]; 

    } 
appDC * application = [dataArray objectAtIndex:[indexPath row]]; 

     //cell.namelbl.text=application.application_name; 


return cell; 
} 
+0

此代碼中的任何一個可以幫助我這個問題? – NullData

+2

恕我直言,如果你接受更多的答案,你會得到更多的幫助。這只是我的經驗。 –

+1

另外,你問如何創建5個不同的自定義單元格或如何更改數據與「股票iOS」UITableViewCells? –

回答

5

試試這個代碼:

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

    static NSString *CellIdentifier = @"Single Cell"; 
    SingleCell *cell =(SingleCell *) [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 


    if (cell == nil) { 
     UIViewController *c = [[UIViewController alloc] initWithNibName:@"SingleCell" bundle:nil]; 
     cell = (SingleCell *) c.view; 
     //[c release]; 

    } 
    appDC * application = [dataArray objectAtIndex:[indexPath row]]; 

    //cell.namelbl.text=application.application_name; 
    if (indexPath.row == 0) 
    { 
     cell.textLabel.text=application.i_name; 
    } 
    else if (indexPath.row == 1) 
    { 
     cell.textLabel.text = application.i_iphone; 
    } 
    else if (indexPath.row == 2) 
    { 
     cell.textLabel.text = application.i_hours; 
    } 
    else if (indexPath.row == 3) 
    { 
     cell.textLabel.text = application.i_address; 
    } 
    else 
    { 
     cell.textLabel.text = application.i_email; 
    } 



    return cell; 
} 

希望這個答案將有所幫助。歡呼聲

+0

我已經試過這個它不需要工作它只給我第一個單元:( – NullData

+0

你可以再試一次嗎?我沒有使用相同的對象來傳遞值textLabel所以,你確定你的對象屬性是NSString的類型嗎? – IKQ

+3

我相信提問者試圖從數組中的每個元素獲取所有數據到一個單元格中......我假設這隻給他第一個單元格的原因是因爲他的數組中只有一個元素,當應用程序嘗試填充表時,它只會給他第一個單元格。定製單元格肯定需要創建以容納從應用程序對象中提取的所有數據數組「dataArray」檢出此鏈接http:// stackoverflow。com/questions/4917027/ios-steps-to-create-custom-uitableviewcell -with-xib-file –

1

IKQ的代碼示例應該可以工作。但我建議嘗試我的TableKit庫。這樣的代碼會更加清晰雅緻:

- (void)viewDidLoad 
{ 
    [super viewDidLoad]; 

    TKStaticCell* nameCell = [TKStaticCell cellWithStyle:UITableViewCellStyleValue1 text:@"Name" detailText:app.i_name]; 
    TKStaticCell* phoneCell = [TKStaticCell cellWithStyle:UITableViewCellStyleValue1 text:@"Phone" detailText:app.i_phone]; 
    TKStaticCell* hoursCell = [TKStaticCell cellWithStyle:UITableViewCellStyleValue1 text:@"Hours" detailText:app.i_hours]; 
    TKStaticCell* addressCell = [TKStaticCell cellWithStyle:UITableViewCellStyleValue1 text:@"Address" detailText:app.i_address]; 
    TKStaticCell* emailCell = [TKStaticCell cellWithStyle:UITableViewCellStyleValue1 text:@"email" detailText:app.i_email]; 
    TKSection* section = [TKSection sectionWithCells:nameCell, phoneCell, hoursCell, addressCell, emailCell, nil]; 
    self.sections = [NSArray arrayWithObject:section]; 
} 

另外,庫允許定義自定義單元格,而不是TKStaticCell

2

我解決了這樣的代碼感謝所有給予建議

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

    static NSString *CellIdentifier = @"CellForInfo"; 
    CellForInfo *cell =(CellForInfo *) [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 


    if (cell == nil) { 
     UIViewController *c = [[UIViewController alloc] initWithNibName:@"CellForInfo" bundle:nil]; 
     cell = (CellForInfo *) c.view; 
     //[c release]; 



    } 

    appDC * application = [dataArray objectAtIndex:0]; 
    if (indexPath.row == 0) 
    { 
     cell.lblinfo.text=application.i_name; 
    } 
    if (indexPath.row == 1) 
    { 
     cell.lblinfo.text = application.i_phone; 
    } 
    if (indexPath.row == 2) 
    { 
     cell.lblinfo.text = application.i_hours; 
    } 
    if (indexPath.row == 3) 
    { 
     cell.lblinfo.text = application.i_address; 
    } 
    if (indexPath.row == 4) 
    { 
     cell.lblinfo.text = application.i_email; 
    } 



    return cell; 
} 
-1
- (void)viewDidLoad 
{ 
    [super viewDidLoad]; 
    // Do any additional setup after loading the view from its nib. 

    [self tableInfo]; 
} 


- (void) tableInfo 
{ 

    NSArray *keysArray = @[@"CampaignsName",@"BusinessName",@"BusinessType",@"CampaignsType",@"Distance",@"Daysleft",@"CampaignImage",]; 

    NSArray *valuesArray1 =[NSArray arrayWithObjects:@"5 % OFF ",@"Coffe Shop",@"1",@"1",@"0.10 Miles",@"5 days Left",@"b2.png", nil]; 

    NSArray *valuesArray2 = [NSArray arrayWithObjects:@"win $2 for you & charity ",@"Red Tommato",@"2",@"2",@"20 Miles",@"2 days Left",@"b1.png", nil]; 

    NSArray *valuesArray3 = [NSArray arrayWithObjects:@"Buy dogs food & get a one more",@"Pet Care",@"3",@"3", @"30 Miles",@"10 days Left",@"b2.png", nil]; 

    NSArray *valuesArray4 =[NSArray arrayWithObjects:@"win $2 for you & charity ",@"Red Tommato",@"1",@"1",@"0.10 Miles",@"7 days Left",@"b2.png", nil]; 



    NSDictionary *dict1 = [NSDictionary dictionaryWithObjects:valuesArray1 forKeys:keysArray]; 

    NSDictionary *dict2 = [NSDictionary dictionaryWithObjects:valuesArray2 forKeys:keysArray]; 

    NSDictionary *dict3 = [NSDictionary dictionaryWithObjects:valuesArray3 forKeys:keysArray]; 

    NSDictionary *dict4 = [NSDictionary dictionaryWithObjects:valuesArray4 forKeys:keysArray]; 



    self.tableArray = [[NSArray alloc]initWithObjects:dict1,dict2,dict3,dict4,dict3,dict1,dict4,dict2,nil]; 
    NSLog(@"Array %@",tableArray); 
    [self.tableObj reloadData]; 
} 

#pragma mark table view datasource 

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

} 

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section 
{ 
    return [self.tableArray count]; 
} 

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

    UITableViewCell *cell = [self tableCustomView:tableView cellForRowAtIndexPath:indexPath]; 
    return cell; 
} 

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



    static NSString *CellIdentifier1 = @"CustomCellIdentifier"; 
    static NSString *CellIdentifier2 = @"CustomCellIdentifier2"; 


    if (indexPath.row % 2 != 0) 
    { 
     CustomCell2 *cell2 = [tableView dequeueReusableCellWithIdentifier:CellIdentifier2]; 
     if (cell2 == nil) 
     { 

      NSArray *topObjects = [[NSBundle mainBundle] loadNibNamed:@"CustomCell2" owner:self options:nil]; 
      cell2 = [topObjects objectAtIndex:0]; 
     } 


     id object = [self.tableArray objectAtIndex:indexPath.row]; 


     cell2.CampaignsNameLbl.text = [NSString stringWithFormat:@"%@",[object valueForKey:@"CampaignsName"]]; 
     cell2.BusinessNameLbl.text = [NSString stringWithFormat:@"%@",[object valueForKey:@"BusinessName"]]; 
     cell2.DistanceLbl.text = [NSString stringWithFormat:@"%@",[object valueForKey:@"Distance"]]; 
     cell2.DaysleftLbl.text = [NSString stringWithFormat:@"%@",[object valueForKey:@"Daysleft"]]; 

     cell2.cellBackImg.image = [UIImage imageNamed:[NSString stringWithFormat:@"%@",[object valueForKey:@"CampaignImage"]]]; 


     int businessType = [[NSString stringWithFormat:@"%@",[object valueForKey:@"BusinessType"]] integerValue]; 

     NSLog(@": %d",businessType); 

     switch (businessType) 
     { 


      case 1: 

       cell2.cellBusinessTypeImg.image = [UIImage imageNamed:@"[email protected]"]; 

       break; 
      case 2: 

       cell2.cellBusinessTypeImg.image = [UIImage imageNamed:@"BTY.png"]; 

       break; 
      case 3: 

       cell2.cellBusinessTypeImg.image = [UIImage imageNamed:@"t1.png"]; 

       break; 

      default: 
       break; 
     } 


     int CampaignsType = [[NSString stringWithFormat:@"%@",[object valueForKey:@"CampaignsType"]] integerValue]; 

     switch (CampaignsType) 
     { 

      case 1: 

       cell2.cellOverLayBusinessTypeImg.image = [UIImage imageNamed:@"t3.png"]; 

       break; 
      case 2: 

       cell2.cellOverLayBusinessTypeImg.image = [UIImage imageNamed:@"t2.png"]; 

       break; 
      case 3: 

       cell2.cellOverLayBusinessTypeImg.image = [UIImage imageNamed:@"t1.png"]; 

       break; 

      default: 
       break; 
     } 

     mainCell = cell2 ; 


    } 

    else 
    { 
     CustomCell1 *cell1 = [tableView dequeueReusableCellWithIdentifier:CellIdentifier1]; 
     if (cell1 == nil) 
     { 

      NSArray *topObjects = [[NSBundle mainBundle] loadNibNamed:@"CustomCell1" owner:self options:nil]; 
      cell1 = [topObjects objectAtIndex:0]; 
     } 


     id object = [self.tableArray objectAtIndex:indexPath.row]; 


     cell1.CampaignsNameLbl.text = [NSString stringWithFormat:@"%@",[object valueForKey:@"CampaignsName"]]; 
     cell1.BusinessNameLbl.text = [NSString stringWithFormat:@"%@",[object valueForKey:@"BusinessName"]]; 
     cell1.DistanceLbl.text = [NSString stringWithFormat:@"%@",[object valueForKey:@"Distance"]]; 
     cell1.DaysleftLbl.text = [NSString stringWithFormat:@"%@",[object valueForKey:@"Daysleft"]]; 

     cell1.cellBackImg.image = [UIImage imageNamed:[NSString stringWithFormat:@"%@",[object valueForKey:@"CampaignImage"]]]; 


     int businessType = [[NSString stringWithFormat:@"%@",[object valueForKey:@"BusinessType"]] integerValue]; 

     NSLog(@": %d",businessType); 

     switch (businessType) 
     { 


      case 1: 

       cell1.cellBusinessTypeImg.image = [UIImage imageNamed:@"[email protected]"]; 

       break; 
      case 2: 

       cell1.cellBusinessTypeImg.image = [UIImage imageNamed:@"BTY.png"]; 

       break; 
      case 3: 

       cell1.cellBusinessTypeImg.image = [UIImage imageNamed:@"t1.png"]; 

       break; 

      default: 
       break; 
     } 


     int CampaignsType = [[NSString stringWithFormat:@"%@",[object valueForKey:@"CampaignsType"]] integerValue]; 

     switch (CampaignsType) 
     { 

      case 1: 

       cell1.cellOverLayBusinessTypeImg.image = [UIImage imageNamed:@"t3.png"]; 

       break; 
      case 2: 

       cell1.cellOverLayBusinessTypeImg.image = [UIImage imageNamed:@"t2.png"]; 

       break; 
      case 3: 

       cell1.cellOverLayBusinessTypeImg.image = [UIImage imageNamed:@"t1.png"]; 

       break; 

      default: 
       break; 
     } 

     mainCell = cell1 ; 
    } 


    return mainCell; 

    NSLog(@"Index Path is :%d %d", indexPath.section,indexPath.row); 
} 
0

您可以使用短

  CellForInfo * cellForInfo = (CellForInfo *)[tableView dequeueReusableCellWithIdentifier:nil]; 

      if (cellForInfo ==nil) { 
       NSArray *nib = [[NSBundle mainBundle] loadNibNamed:@"CellForInfo" owner:self options:nil]; 
       cellForInfo = [nib objectAtIndex:0]; 
      } 
相關問題