0

Code:

In .h:

    NSMutableArray *contentArray;

I'm declaring my array.


In .m

    - (void)viewDidLoad
    {
        [super viewDidLoad];

        contentArray = [[NSMutableArray alloc] initWithObjects:@"view", @"browse", @"create", nil];

        // Uncomment the following line to preserve selection between presentations.
        // self.clearsSelectionOnViewWillAppear = NO;

        // Uncomment the following line to display an Edit button in the navigation bar for this view controller.
        // self.navigationItem.rightBarButtonItem = self.editButtonItem;
    }

I'm setting it up in my view did load.

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

I'm setting the number of rows to the array 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] autorelease];
        }

        // Configure the cell...

        [[cell textLabel] setText:[contentArray objectAtIndex:indexPath.row]];

        return cell;
    }

Nothing. But if I do "[[cell textLabel] setText:@"Hello World."];" instead, it works fine.

5 Answers 5

1

Are you at least getting 3 empty rows in table ? If yes then just change your code

NSString *tempString = [your_array objectAtIndex:indexPath.row];
cell.textLabel.text = tempString; 

If you are not even getting empty strings then make property of your array in .h file. Synthesize it in .m (also release it in delloc function) and finally in viewDidLoad right following

NSMutableArray tempContentArray = [[NSMutableArray alloc] arrayWithObjects:@"view", @"browse", @"create", nil];
self.contentArray=tempArray;

and then write following code to get cell title

NSString *tempString = [self.contentArray objectAtIndex:indexPath.row];
cell.textLabel.text = tempString; 
Sign up to request clarification or add additional context in comments.

Comments

0

Try to initialize your Array in your init method or call reloadDataon your UITableView after you set your Array.

1 Comment

Thanks! I just realized that, since i'm not using a nav controller, viewDidLoad is never called! Just had to stick in in my init. Thanks again!
0

Your code in .m file is fine. Write property .h file synthesize and dealloc your NSMutable array .m file it may be help you.

Comments

0

simply use following line of code //initlize nsarray with objects.

-(void)viewDidLoad
{
NSArray *practice_ArrayForDisplayingEx=[NSArray alloc]initWithObjects:@"bhupi",@"bhupi",@"bhupi",@"bhupi",nil];

}


//then use in tableview.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *simpleTableIdentifier = @"SimpleTableItem";
   // UIButton *practice_takeTest_button;

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier];

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


    cell.textLabel.text =[practice_ArrayForDisplayingEx objectAtIndex:indexPath.row];
    cell.textLabel.font=[UIFont fontWithName:@"Arial" size:15];



    return cell;
}

make sure you included UITableViewDelegate & UITableViewDataSourse prtocol in .h file.

hope it will help you..

Comments

0

Make sure that your .h implements the tableview delegate and datasource... should look like this

 @interface myClass : UITableViewController <UITableViewDelegate, UITableViewDataSource>

then make this code in your ViewDidLoad method

 [self.tableView setDelegate:self];
 [self.tableView setDataSource:self];

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.