Table Basics Question - UITableViewCell

Discussion in 'iOS Programming' started by newtomac123, Jan 8, 2014.

  1. newtomac123, Jan 8, 2014
    Last edited by a moderator: Jan 8, 2014

    newtomac123 macrumors newbie

    Joined:
    Jan 8, 2014
    #1
    I apologize, but this is eating my lunch. I'm new to Objective C programming, but I'm just getting frustrated with trying to figure out this issue. I'm currently just putting together a little table view application together through a tutorial.

    Code:
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
        if (cell == nil) {
            cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier: CellIdentifier];
            if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPhone ) {
                cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
            }
        }
    The program will build, but will not run (keeps booting me out). I have only 1 error at =

    Code:
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    The error = Semantic Issue: Local declaration of 'tableView' hides instance variable.

    Anyone have a quick fix for this error?
     
  2. waterskier2007 macrumors 68000

    waterskier2007

    Joined:
    Jun 19, 2007
    Location:
    White Lake, MI
    #2
    At any point do you instantiate a variable with something like:

    Code:
    ClassName *tableView = [[ClassName alloc] init]...
    
    *where ClassName is some class like UITableView or something else?

    If so, you probably have a property in your class that is 'tableView' so you shouldn't instantiate a variable with the same name.
     
  3. dejo Moderator

    dejo

    Staff Member

    Joined:
    Sep 2, 2004
    Location:
    The Centennial State
    #3
    First, welcome to the exciting world of iOS development! Have fun while you're here.

    Second, what tutorial? Please be as specific as possible. This will help provide us context.

    Third, please provide the complete method, if possible, as well as the name and .h of the class your method is in. Also, please provide your instance variables.

    Fourth, where do you declare tableView?
     
  4. newtomac123, Jan 9, 2014
    Last edited by a moderator: Jan 9, 2014

    newtomac123 thread starter macrumors newbie

    Joined:
    Jan 8, 2014
    #4
    UITableView

    Thank you all so much for the help!

    I'm currently using "Tutorial for Xcode 5 Lite" - Currently on #5 table views

    so as for my Viewcontroller.h:

    Code:
    #import <UIKit/UIKit.h>
    
    @interface ViewController : UIViewController
    
    @property(nonatomic,retain) IBOutlet UITableView *tableView;
    
    @end
    
    Viewcontroller.m :

    Code:
    #import "ViewController.h"
    
    @interface ViewController (){
        
        NSArray *soundTitles;
        
    }
    
    @end
    
    @implementation ViewController
    
    @synthesize tableView;
    
    - (void)viewDidLoad
    {
        [super viewDidLoad];
    	// Do any additional setup after loading the view, typically from a nib.
        
        
        soundTitles = [[NSArray alloc] initWithObjects:@"Singing  Birds",@"Wind Chimes",@"Falling Rain",@"Ocean",@"Whale Song", nil];
    }
    
    #pragma mark -
    #pragma mark Table View data source
    
    - (NSInteger) numberOfSectionsInTableView:(UITableView *) tableView{
        
        return 1;
        
    }
    
    - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
        
        return [soundTitles count];
        
    }
    
    - (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];
            if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPhone ) {
                cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
            }
        }
        
    
        cell.textLabel.text = [soundTitles objectAtIndex:indexPath.row];
        
        return cell;
        
    }
    
    #pragma mark - 
    #pragma mark Table View Delegate
    
    - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
        
        NSLog(@"Did select row at index path");
        NSLog(@"Row %d",indexPath.row);
        
    }
    
    - (void)didReceiveMemoryWarning
    {
        [super didReceiveMemoryWarning];
        // Dispose of any resources that can be recreated.
    }
    
    @end
    
    I apologize for the length, I just wanted to include everything.

    Is:
    Code:
    @property(nonatomic,retain) IBOutlet UITableView *tableView;
    the wrong decleration?
     
  5. Ubuntu macrumors 68000

    Ubuntu

    Joined:
    Jul 3, 2005
    Location:
    UK/US
    #5
    You should either rename the tableView you've defined (the IBOutlet in that last code snippet) or I think you can change the parameter name in the UITableView delegate & datasource methods, as so;


    Code:
    - (void)tableView:(UITableView *)myTableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath 

    Personally I'd do the former, as I always prefer to keep my variable names contextual. So if this tableview is storing a list of contacts, I'd have the following declaration;

    Code:
    @property(nonatomic,retain) IBOutlet UITableView *contactsTableView;
     
  6. newtomac123, Jan 11, 2014
    Last edited: Jan 11, 2014

    newtomac123 thread starter macrumors newbie

    Joined:
    Jan 8, 2014
    #6
    So I'm still having the same problem..... I'm obviously missing something.. I apologize... My header is now:


    Code:
    
    #import <UIKit/UIKit.h>
    
    @interface ViewController : UIViewController
    
    @property(nonatomic,retain) IBOutlet UITableView *tableView;
    
    @end
    
    And my .m file is:
    Code:
    #import "ViewController.h"
    
    @interface ViewController (){
        
        NSArray *soundTitles;
        
    }
    
    @end
    
    @implementation ViewController
    
    @synthesize tableView;
    
    - (void)viewDidLoad
    {
        [super viewDidLoad];
    	// Do any additional setup after loading the view, typically from a nib.
        
        
        soundTitles = [[NSArray alloc] initWithObjects:@"Singing  Birds",@"Wind Chimes",@"Falling Rain",@"Ocean",@"Whale Song", nil];
    }
    
    #pragma mark -
    #pragma mark Table View data source
    
    - (NSInteger) numberOfSectionsInTableView:(UITableView *) tableView{
        
        return 1;
        
    }
    
    - (NSInteger)otableView:(UITableView *)otableView numberOfRowsInSection:(NSInteger)section{
        
        return [soundTitles count];
        
    }
    
    - (UITableViewCell *)otableView:(UITableView *)otableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
    {
    
        static NSString *CellIdentifier = @"Cell";
     
        UITableViewCell *cell = [otableView dequeueReusableCellWithIdentifier:CellIdentifier];
        if (cell == nil) {
            cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier: CellIdentifier];
            if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPhone ) {
                cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
            }
        }
        
    
        cell.textLabel.text = [soundTitles objectAtIndex:indexPath.row];
        
        return cell;
        
    }
    
    #pragma mark - 
    #pragma mark Table View Delegate
    
    - (void)otableView:(UITableView *)otableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
        
        NSLog(@"Did select row at index path");
        NSLog(@"Row %d",indexPath.row);
        
    }
    
    - (void)didReceiveMemoryWarning
    {
        [super didReceiveMemoryWarning];
        // Dispose of any resources that can be recreated.
    }
    
    @end
    
    It says "no issues at the top", but builds, but once the app starts it boots me out with the error in the output:

    2014-01-11 13:16:35.810 TableViewControllerBegin[1962:70b] -[ViewController tableView:numberOfRowsInSection:]: unrecognized selector sent to instance 0x8a42950
    2014-01-11 13:16:35.816 TableViewControllerBegin[1962:70b] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[ViewController tableView:numberOfRowsInSection:]: unrecognized selector sent to instance 0x8a42950'
    libc++abi.dylib: terminating with uncaught exception of type NSException
    (lldb)


    So I changed it back to how it originally was, and only changed the line in the header to:
    Code:
    @property(nonatomic,retain) IBOutlet UITableView *oTableView;
    
    and then the line in the .m file to:
    Code:
    @synthesize otableView;
    
    and it gave me this error:

    2014-01-11 13:26:19.921 TableViewControllerBegin[2153:70b] *** Terminating app due to uncaught exception 'NSUnknownKeyException', reason: '[<ViewController 0x8b3c3c0> setValue:forUndefinedKey:]: this class is not key value coding-compliant for the key tableView.'

    libc++abi.dylib: terminating with uncaught exception of type NSException
    (lldb)

    Any ideas? I apologize this is taking multiple posts...
    Thank you so much for the help again!
     
  7. dejo Moderator

    dejo

    Staff Member

    Joined:
    Sep 2, 2004
    Location:
    The Centennial State
    #7
  8. dejo Moderator

    dejo

    Staff Member

    Joined:
    Sep 2, 2004
    Location:
    The Centennial State
    #8
    If you change the name of an IBOutlet variable, you usually need to reconnect it in IB.
     

Share This Page