-2

In my app, I have created a mutable array named "array".

 array=[[NSMutableArray alloc]initWithObjects:@"a",@"b",@"c",@"d",@"e", nil];  

My tableView contains only one row for multiple section, Each section has a customView which contains only one label.
My custom View name is ContentOfCell

ContentOfCell * contentOfCell=[[ContentOfCell alloc]initWithFrame:CGRectMake(0, 0, 100, 50)];
    [cell.contentView addSubview:contentOfCell];  

I have added the array to the label

contentOfCell.label.text=[array objectAtIndex:indexPath.section];  

My problem is that it recognises only the first 4 values in the array and the first 4 values is getting repeated in the sections

I thing something is going wrong here

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
return [indexPath row]+50;
} 

If I change 50 by 100, the error occurs if not the values are inserted correctly

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
NSString * cellIdentifier=[NSString stringWithFormat:@"MyTableView"];
cell=[myTableView dequeueReusableCellWithIdentifier:cellIdentifier];
if(cell== nil)
{
    cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];
ContentOfCell * contentOfCell=[[ContentOfCell alloc]initWithFrame:CGRectMake(0, 0, 100, 50)];
    [cell.contentView addSubview:contentOfCell];
   contentOfCell.nameField.text=[arr objectAtIndex:indexPath.section];
    cell.contentView.backgroundColor=[UIColor whiteColor];
 }}

enter image description here enter image description here Here the view contains only 3 rows, when I scroll down the array is initiated again, then the values are printed again

chandru
  • 407
  • 1
  • 5
  • 26

5 Answers5

2

I guess you are not reusing the cell correctly.

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
   NSString * cellIdentifier=[NSString stringWithFormat:@"MyTableView"];
   cell=[myTableView dequeueReusableCellWithIdentifier:cellIdentifier];
   ContentOfCell *contentOfCell;
   //Initialization Part. declare objects here
   if(cell== nil)
   {
      //Creation part. Create your new cell here. Do all UI actions here
      cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];
      contentOfCell=[[ContentOfCell alloc]initWithFrame:CGRectMake(0, 0, 100, 50)];
      contentOfCell.tag =100;
      [cell.contentView addSubview:contentOfCell];
      cell.contentView.backgroundColor=[UIColor whiteColor];
   }
   else{
      // Reusable part. Reuse the UI controls here from existing cell
      contentOfCell = (ContentOfCell *)[cell.contentView viewWithTag:100];
   }
   //Assign all the data here
   contentOfCell.nameField.text=[arr objectAtIndex:indexPath.section];
}

This is one of the simple mistake often committed by iOS Rookies. Kindly check the Apple developer documentation here to understand how a tableview cell is reused https://developer.apple.com/library/ios/documentation/userexperience/conceptual/tableview_iphone/TableViewCells/TableViewCells.html

ipraba
  • 16,485
  • 4
  • 59
  • 58
  • can U please check this link, I have given comments – chandru Jan 02 '14 at 11:00
  • http://stackoverflow.com/questions/20880074/ios-error-in-parsing-the-data/20880308?noredirect=1#comment31335993_20880308 – chandru Jan 02 '14 at 11:24
  • I want use the coordinates to plot them on the map from the dictionary get me an answer in that page, – chandru Jan 02 '14 at 11:34
  • k, can U tell me wat mistake i did in this table view – chandru Jan 02 '14 at 11:37
  • Its simple you haven't reused the cell properly. Reusable concept is somewhat big to explain here thats why i gave you the link of apple doc. Kindly refer that or google for how UItableviewcell works. http://www.mobilesce.com/2011/12/27/the-best-way-to-do-custom-reusable-uitableviewcells/ – ipraba Jan 02 '14 at 11:43
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/44317/discussion-between-chandru-and-iprabu) – chandru Jan 02 '14 at 11:44
2

I implemented same code as below with grouped table.Everything is fine.Please compare your code with below code.

- (void)viewDidLoad
{
[super viewDidLoad];
arrData=[[NSMutableArray alloc] initWithObjects:@"a",@"b",@"c",@"d",@"e",@"f",@"g",@"h",@"i",@"j",@"k", nil];

// Do any additional setup after loading the view, typically from a nib.
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
   return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{ 
   return [arrData count];
}
 - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath;
{
   return 50.0;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *aCell=[tableView dequeueReusableCellWithIdentifier:@"da"];
    if(aCell==Nil)
    {
      aCell=[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"da"];

      ContentOfCell *ContentOfCellObj=[[ContentOfCell alloc] initWithFrame:CGRectMake(0,0,320,50)];
      ContentOfCellObj.tag=101;
      [aCell.contentView addSubview:ContentOfCellObj];
     }
     ContentOfCell *ContentOfCellObj=(ContentOfCell*)[aCell.contentView viewWithTag:101];
     ContentOfCellObj.nameField.text=[NSString stringWithFormat:@"%@",[arrData objectAtIndex:indexPath.section]];

     return aCell;
}
Rajesh Loganathan
  • 11,129
  • 4
  • 78
  • 90
cjd
  • 549
  • 3
  • 11
1

Use this:

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString * cellIdentifier=@"CellIdentifier";//or your custom name
    UITableViewCell* cell=[tableView dequeueReusableCellWithIdentifier:cellIdentifier];
    if(cell== nil)
    {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];
        ContentOfCell * contentOfCell=[[ContentOfCell alloc]initWithFrame:CGRectMake(0, 0, 100, 50)];

        contentOfCell.tag = 101;
        [cell.contentView addSubview:contentOfCell];
        cell.contentView.backgroundColor=[UIColor whiteColor];
     }

     ContentOfCell * contentOfCell = (ContentOfCell*)[cell.contentView viewWithTag:101];

     contentOfCell.nameField.text=[arr objectAtIndex:indexPath.section];

     return cell;
}
Bhumeshwer katre
  • 4,671
  • 2
  • 19
  • 29
0

Make sure you set

myTableView.dataSource = self;

and these two delegate methods

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
   return [arrData count];
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{ 
   return 1;
}
Giridhar
  • 104
  • 1
  • 6
0

Try this code, surely it will resolve your problem.

Step 1. First declare array as global variable

@interface YOUR-CLASS-NAME ()
{
     NSMutableArray *array
}

Step 2. Now set no of sections you need. This means how many time do you want to repeat the array values in table continuously.

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

Step 3. Now set no of rows you need to display from your array. This means no of values from your array need to display

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return [array count];
}
Rajesh Loganathan
  • 11,129
  • 4
  • 78
  • 90