Loading a UITableViewCell subclass using a XIB file

I am having trouble getting my CustomTableViewCell, a subclass of UITableViewCell to appear in my table view.

I am using a xib to represent that cell, but I am assuming that the code for the data source delegate doesn't change. I made sure to set an identical reuse identifier inside the table view cell XIB.

I isolated the problem to the fact that the datasource method that returns the table cell isn't working correctly, here it is:

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

    DataObject *foo = [self.dataArray objectAtIndex:indexPath.row];

    if (cell == nil) 
    {
        cell = [[CustomTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
    }

    [[cell overview] setText:foo.overview];
    [[cell price] setText:foo.price];
    NSLog(@"cell initialized, description text is %@",cell.overview.text);

    return cell;
}

Not sure why this isn't working, but that last log statement always prints a (null) at the end, and yes I did verify that the overview property of the data object has a valid string in it. Same thing for price.

Answers


In .h file, you write it.

IBOulet CustomTableViewCell *tableCell;

and connect to File's Owner in CustomTableViewCell's .xib file.

You modify this in (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath

if (cell == nil) 
 {
     [[NSBundle mainBundle] loadNibNamed:@"CustomTableViewCell" owner:self options:nil];
     cell = tableCell;
 }

I think it will be helpful to you.


1) Make your Custom cell class separate

2) XIB : File Owner class change to NSObject

3) XIB : UITableViewCell change to MyTableViewCell

4) Where you want to add inside table view code as

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

    static NSString *CellIdentifier = @"MyCell";

    MyTableViewCell *cell = (MyTableViewCell*) [tableView dequeueReusableCellWithIdentifier:CellIdentifier];

    if (cell == nil) {

        NSArray *arrayCellXib = [[NSBundle mainBundle] loadNibNamed:@"MyTableViewCell" 
                                                              owner:self 
                                                            options:nil];
        if (indexPath.row == 0) {
            // first table cell
            cell = [arrayCellXib objectAtIndex:0]; // *
            ...... 
         }else {

            cell = [arrayCellXib objectAtIndex:1]; // *
         }
        ...
      }
   return cell;

}

Object at index 0,1 ... are index for which order you make MyTableViewCell in xib, if you want to have more than one custom cell. means in a MyTableViewCell class u make unlimited custom cell and use according to requirement.

Normally, just make ONE custom cell in the XIB and do exactly this:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
    {
    int thisRow = indexPath.row;
    NSString *exampleText = [yourData objectAtIndex:thisRow];

    static NSString *CellID = @"cu5";

    FancyCell *cell =
       (FancyCell *)[tableView dequeueReusableCellWithIdentifier:CellID];
    if (cell == nil)
        {
        NSArray *cellTeam =
          [[NSBundle mainBundle] loadNibNamed:@"FancyCell"
             owner:self options:nil];
        cell = [cellTeam objectAtIndex:0];
        }

    [cell someFunction:exampleText];
    [cell.someLabel setText:exampleText];
    return cell;
    }

Hoping may help u :)


Need Your Help

ErrorDocument is not working for 401 response

.htaccess redirect

I am trying to redirect to a custom error page using a redirect in .htaccess. Please check the site jobslanda.com. The line ErrorDocument 401 401.php is not working. I am using the following

Amazon Web Service for Alexa

amazon-web-services alexa

AWS (Amazon Web Service) provides an API for submitting HTTP requests to Alexa.