Reputation: 103
now I a beginner in IOS programming, I created a view and placed a tableview inside it, then I created a xib with the template I want to use for the tablecell and made a class for that call. the table cell has a label (set on load of the cell), a switch and a textfield.
The table loads fine, with the labels showing the correct text they should, there is 12 rows that are loaded from the database. sqLite.
the problem: when I edit the textfield in row 1, the text field in row 9 is edited, and vise versa. when I edit in row 2, row 10 is edited! upto row 4,12 not only the text field, but also toggling the switch toggles.
Some code: AlertViewController.h
@property (weak, nonatomic) IBOutlet UITableView *table;
AlertViewController.m
@synthesize table;
static NSString *alertCellID=@"AlertViewCell";
----some code here ----
-(void)ViewDidLoad
{
---some code here----
self.table.delegate=self;
self.table.dataSource=self;
if(managedObjectContext==nil)
{
id appDelegate=(id)[[UIApplication sharedApplication] delegate];
managedObjectContext=[appDelegate managedObjectContext];
}
[self loadCategories];
[table registerNib:[UINib nibWithNibName:@"AlertViewCell" bundle:[NSBundle mainBundle]] forCellReuseIdentifier:alertCellID];
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return [arrCategories count];
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
Category *cat=[arrCategories objectAtIndex:indexPath.row];
AlertViewCell *cell = [tableView dequeueReusableCellWithIdentifier:alertCellID];
UILabel *lblGroupName=(UILabel *)[cell viewWithTag:101];
lblGroupName.text=cat.nameEn;
UITextField *txtHours=(UITextField *)[cell viewWithTag:103];
txtHours.delegate=self;
return cell;
}
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return 1;
}
Edit: I dont know is it related to the scroll size fitting 8 records? and how to overcome that?
Upvotes: 1
Views: 893
Reputation: 103
Ok I solved it as follows:
First the issue was because i m using dequeuereusablecellwithidentifier, which seems to return the same reference every x number of rows, where x is the number of rows that appear on the screen without scrolling, so this made cell 1=cell 9, cell 2=cell 10 and so on.
so to solve it, I had to make the identifier unique, and to solve the issue that the identifier was used to load the registered nib, I had to register the nib with this unique identifier names.. here is the changes:
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
NSString *identifier=[NSString stringWithFormat:@"%@%d",alertCellID,indexPath.row];
[table registerNib:[UINib nibWithNibName:@"AlertViewCell" bundle:[NSBundle mainBundle]] forCellReuseIdentifier:identifier];
Category *cat=[arrCategories objectAtIndex:indexPath.row];
AlertViewCell *cell = (AlertViewCell *)[tableView dequeueReusableCellWithIdentifier:identifier forIndexPath:indexPath];
if(!cell)
{
NSArray *topLevelObjects=[[NSBundle mainBundle] loadNibNamed:@"AlertViewCell" owner:self options:nil];
cell=[topLevelObjects objectAtIndex:0];
}
UILabel *lblGroupName=(UILabel *)[cell viewWithTag:101];
lblGroupName.text=cat.nameEn;
UITextField *txtHours=(UITextField *)[cell viewWithTag:103];
txtHours.delegate=self;
return cell;
}
Upvotes: 1