UITableView cells mixed when scrolling - ios

While I realise this is a popular problem among developers and there are many similar questions on Stack Overflow, none have been able to help me resolve this. There is a strange issue with a UITableView in which the individual cells are becoming mixed up, or seemingly mixed up. I believe I am reusing cells correctly and simply need a second set of eyes. Here is my code:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *simpleTableIdentifier = #"SimpleTableCell";
SimpleTableCell *cell = (SimpleTableCell *)[tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier];
if (cell == nil)
{
NSArray *nib = [[NSBundle mainBundle] loadNibNamed:#"SimpleTableCell" owner:self options:nil];
cell = [nib objectAtIndex:0];
}
cell.friendFullName.text = [users objectAtIndex:indexPath.row];
return cell;
}
I am using dynamic, custom cells in a seperate UITableCell.

You cell is being reused. Set your label in willDisplayCell instead of cellForRowAtIndexPath.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *simpleTableIdentifier = #"SimpleTableCell";
SimpleTableCell *cell = (SimpleTableCell *)[tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier];
if (cell == nil)
{
NSArray *nib = [[NSBundle mainBundle] loadNibNamed:#"SimpleTableCell" owner:self options:nil];
cell = [nib objectAtIndex:0];
}
return cell;
}
- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath
{
SimpleTableCell *simpleTableCell = (SimpleTableCell *)cell;
simpleTableCell.friendFullName.text = [users objectAtIndex:indexPath.row];
}

Related

how to resolve error- property 'text' not found on 'object' of type UITableView in ios

I'm getting an error in the below code. ..L1name , L1email , L1code are the property names for the array nameData, nameEmail, nameCode.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *simpleTableIdentifier = #"SimpleTableCell";
SimpleList1 *cell = (SimpleList1 *)[tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier];
if (cell == nil) {
NSArray *nib = [[NSBundle mainBundle] loadNibNamed:#"SimpleTableCell" owner:self options:nil];
cell = [nib objectAtIndex:0];
}
cell.L1name.text = [nameData objectAtIndex:indexPath.row];
cell.L1email.text = [nameEmail objectAtIndex:indexPath.row];
cell.L1code.text = [nameCode objectAtIndex:indexPath.row];
return cell;
}
I guess there is a typo in your question title and the error is actually referencing UITableViewCell. This would be because UITableViewCell doesn't have your custom properties.
You need to ensure you have created an instance of your custom cell subclass, and that the variable is of the correct class type so the compiler knows what properties are available (casting if required):
MYCell *cell = (MYCell *)[tableView dequeueReusableCellWithIdentifier:...];

Automatically select wrong cell in UITableView?

My Problem is, When I select a cell it automatically select other cell also.
Also when I deselect that selected cell, automatically selected cell also be deselect.
Any help is much appreciated.
Thank you.
My Code is :
#pragma mark
#pragma mark UITableViewDelegate
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
return 1;
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
return [cellDataArray count];
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
static NSString *cellIdentifier = #"customCellSearchBySpeciality";
SpecialityCustomCell *cell = (SpecialityCustomCell *)[tableView dequeueReusableCellWithIdentifier:cellIdentifier];
if (cell == nil)
{
NSArray *nib = [[NSBundle mainBundle] loadNibNamed:#"SearchBySpecialityViewController" owner:self options:nil];
cell = [nib objectAtIndex:0];
}
cell.cellData.text = [cellDataArray objectAtIndex:indexPath.row];
return cell;
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
SpecialityCustomCell *selectedCell = (SpecialityCustomCell *)[tableView cellForRowAtIndexPath:indexPath];
if (selectedCell.selectedCellImageView.image) {
selectedCell.selectedCellImageView.image = nil;
}
else{
[selectedCell.selectedCellImageView setImage:[UIImage imageNamed:#"image"]];
}
}
I think you need to maintain instances of which cell you had aaded on each row then do selection and deselection on that particular object of cell's imageview like below,
NSMutableArray *cellArray;
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
static NSString *cellIdentifier = #"customCellSearchBySpeciality";
SpecialityCustomCell *cell = (SpecialityCustomCell *)[tableView dequeueReusableCellWithIdentifier:cellIdentifier];
[cellArray addObject:cell];
if (cell == nil)
{
NSArray *nib = [[NSBundle mainBundle] loadNibNamed:#"SearchBySpecialityViewController" owner:self options:nil];
cell = [nib objectAtIndex:0];
}
cell.cellData.text = [cellDataArray objectAtIndex:indexPath.row];
return cell;
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
for(int cc=0;cc<[cellArray count];cc++){
if(indexPath.row==cc){
if ([cellArray objectAtIndex:cc].selectedCellImageView.image) {
[cellArray objectAtIndex:cc].selectedCellImageView.image = nil;
}
else{
[[cellArray objectAtIndex:cc].selectedCellImageView setImage:[UIImage imageNamed:#"image"]];
}
}
}

Reuse Custom Cell using NIB

Hi I'm new to IOS developement and I created a UITableView which uses custom cells that are created in a nib. Below is my code from my ViewController that is loading the cells however if I scroll up and down 3 times the app crashes because I don't think I'm reusing the cells correctly. I googled around but much of the code/solutions I found seemed to be outdated. My code is below any help is greatly appreciated!
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = #"CustomCell";
CustomCell *cell = (CustomCell *) [tableView dequeueReusableCellWithIdentifier:#"cellIdentifier"];
if (cell == nil) {
NSArray *topLevelObjects = [[NSBundle mainBundle] loadNibNamed:#"CustomCell" owner:self options:nil];
for (id currentObject in topLevelObjects){
if ([currentObject isKindOfClass:[UITableViewCell class]]){
cell = (CustomCell *) currentObject;
break;
}
}
}
cell.TITLE.text = [NSString stringWithFormat:#"\"%#\"", [TITLE objectAtIndex:indexPath.row]];
cell.desc.text = [desc objectAtIndex:indexPath.row];
cell.votes.text = [votes objectAtIndex:indexPath.row];
return cell;
}
change the row
CustomCell *cell = (CustomCell *) [tableView dequeueReusableCellWithIdentifier:#"cellIdentifier"];
to be
CustomCell *cell = (CustomCell *) [tableView dequeueReusableCellWithIdentifier: CellIdentifier];
Go to your CustomCell .xib file in IB, look for the identifier field and set it to CustomCell
You can register the nib for tableView, like :
(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *cellIdentifier = #"CustomCell";
CustomCell *cell = (CustomCell *) [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
if (cell == nil) {
[tableView registerNib:[UINib nibWithNibName:#"CustomCell" bundle:nil] forCellReuseIdentifier:cellIdentifier];
cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
}
//...
return cell;
}

Very strange issue in cellForRowAtIndexPath

I really can't understand why i get a "EXC_BAD_ACCESS" in the line where I assign NSArray *topLevelObjects. It's crazy because I use exactly the same code and the same BlogCell in another tabliView, and there it's working perfectly!
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
int r = indexPath.row;
static NSString *CellIdentifier = #"Blog";
BlogCell *cell = (BlogCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
NSArray *topLevelObjects = [[NSBundle mainBundle] loadNibNamed:#"BlogCell" owner:self options:nil];
cell = [topLevelObjects objectAtIndex:0];
}
return cell;
}
Make sure all the outlets you specify in BlogCell to FilesOwner have corresponding IBOutlets in this class.

Second Cell appearing only after first cell is selected in Tableview ios

When I run my ios app, only the first cell displays in the TableView, then when clicked the second cell displays. I would like them both to display at the same time, but I cannot figure out this behavior.
Here is the code for my view did load
- (UITableViewCell *)tableView:(UITableView *)tableView
cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = #"ProductCellId";
ProductTableCell *cell =
(ProductTableCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
[[NSBundle mainBundle] loadNibNamed:#"ProductTableCell" owner:self options:nil];
cell = self.productCell;
}
Product *product = [products objectAtIndex:indexPath.row];
[cell configureForProduct:product];
return cell;
}
Any help would be much appreciated!
Since you are loading your cell from Interface builder try this:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath: (NSIndexPath *)indexPath {
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:#"ProductTableCell"];
if (cell == nil) {
// Load the top-level objects from the custom cell XIB.
NSArray *topLevelObjects = [[NSBundle mainBundle] loadNibNamed:#"ProductTableCell" owner:self options:nil];
// Grab a pointer to the first object (presumably the custom cell, as that's all the XIB should contain).
cell = [topLevelObjects objectAtIndex:0];
}
Product *product = [products objectAtIndex:indexPath.row];
[cell configureForProduct:product];
return cell;
}
Change this:
static NSString *CellIdentifier = #"ProductCellId";
to:
static NSString *CellIdentifier = #"ProductTableCell";
You can find additional information for what you are trying to do in this question: Load cells from XIB files

Resources