Show data in Custom tableview Cell

84 views Asked by At

I am new to developing. i have some experience but i'm not great at it (yet). I'm working on this tableview app which shows data. I want to show that data in a custom cell. But i don't know how to do that. This is the code i use for my tableview:

}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    HistoryTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"HistoryViewTableCell"];
    if (cell == nil) {

        NSArray *topLevelObjects = [[NSBundle mainBundle] loadNibNamed:@"HistoryViewTableCell" owner:self options:nil];

        cell = [topLevelObjects objectAtIndex:0];
    }
    NSDictionary * tempDictionary = [tableData objectAtIndex:indexPath.row];
    cell.textLabel.text = [tempDictionary objectForKey:@"PickupAddress"];

    return cell;
}

I need to show this line of code in my custom cell.

cell.textLabel.text = [tempDictionary objectForKey:@"PickupAddress"];

I already made a class and a xib file for the custom cell

2

There are 2 answers

0
Himanshu gupta On

This may solve the problem:

 HistoryTableViewCell *cell = (HistoryTableViewCell *)[tableView dequeueReusableCellWithIdentifier:@"HistoryViewTableCell"];
0
Code Hunterr On

your written kind of code will not work. if you are using custom XIB than add a UILable in the xib. and than assign them tag number.

now from ViewController do the following:

  1. write this after @implementation

     enum {
      CELL_TITLE = 2,
      CELL_SUBTITLE_1 = 3,
     };
    
  2. Load the Custom XIB File in cellforrowatindexpath method.

    // Get table View Cell
    static NSString *CellIdentifier = @"customCell";
    UITableViewCell *cell = [atableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
    
        NSArray *nib;
        nib = [[NSBundle mainBundle] loadNibNamed:CellIdentifier owner:self options:nil];
        cell = (UITableViewCell *) [nib objectAtIndex:0];
    }
    
  3. Now Add text on Lable with the below methods;

    UILabel *titleLable = (UILabel *) [cell.contentView viewWithTag:CELL_TITLE];
    titleLable.text = @"This is your text";