bardockyo
bardockyo

Reputation: 1435

Cannot seem to pass an array to uitableview xcode

I am trying to display the contents of an array onto a uitableview. It works fine if I assign the values individually but when I try to display them from an array xcode exits with nothing in the debugging report except (lldb). It compiles and runs great until I go to the page with the tableview. I've looked up the error and it seems to pertain to memory allocation but I am unable to manually release anything due to having to have ARC enabled for some JSON classes to operate. Can anyone take a look and see what possibly might be the issue.

- (void)viewDidLoad
{
    [super viewDidLoad];
    theArray = [[NSArray alloc] initWithObjects:@"1","2","3","4",nil];

    // Do any additional setup after loading the view.
}


#pragma mark Table view methods

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

// Customize the number of rows in the table view.
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return [theArray count];
}

// Customize the appearance of table view cells.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {



    static NSString *CellIdentifier = @"Cell";

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];


    // Set up the cell...
    self.recentSearchesTable.backgroundColor =[UIColor clearColor];
    self.recentSearchesTable.separatorColor = [UIColor clearColor];
    cell.textLabel.font = [UIFont fontWithName:@"Helvetica" size:15];
    cell.textLabel.text = [theArray objectAtIndex:indexPath.row];

    cell.contentView.backgroundColor = [UIColor clearColor];

    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    // open a alert with an OK and cancel button
    NSString *alertString = [NSString stringWithFormat:@"Clicked on row #%d", [indexPath row]];
    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:alertString message:@"" delegate:self cancelButtonTitle:@"Done" otherButtonTitles:nil];
    [alert show];

}

Upvotes: 2

Views: 1865

Answers (3)

Ilker Baltaci
Ilker Baltaci

Reputation: 11779

First of all you do not need to disable ARC for your project due to an external class.You can exclude these classes by setting -fno-objc-arc flag.

Back to your answer You should alloc a new cell if it can be reeused from a previous one.You should alloc you new cell if dequeueReusableCellWithIdentifier:CellIdentifier does not return a cell.

static NSString *CellIdentifier = @"Cell";

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];

  if (cell == nil)  {
    cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:Cellidentifier] autorelease];
  }

Apart from that when you init your datasource array in viewDidLoad you need to add @ before each element to indicate NSString.

Upvotes: 1

Nirav Jain
Nirav Jain

Reputation: 5107

You did two mistake here first you need to initialize array in this way.

theArray = [[NSArray alloc] initWithObjects:@"1",@"2",@"3",@"4",nil];

and another mistake is you need to initialize cell when it is nil

if (cell == nil) {
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
        cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
    }

if you want whole code then like this way you need to implement.

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



    static NSString *CellIdentifier = @"Cell";

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
        cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
    }
    // Set up the cell...

    cell.textLabel.text = [theArray objectAtIndex:indexPath.row];

    cell.contentView.backgroundColor = [UIColor clearColor];

    return cell;
}

Upvotes: 1

thelaws
thelaws

Reputation: 8001

In viewDidLoad your array contains non-object items, change it too:

theArray = [[NSArray alloc] initWithObjects:@"1",@"2",@"3",@"4",nil];

Note the @ before each item in the array, indicating that they're NSString literals (and not c strings).

Upvotes: 4

Related Questions