UITableView crashes at cellForRowAtIndexPath with error "EXC_BAD_ACCESS" - ios

One of my ViewControllers has a UITableView inside of it, and it crashes when it loads the cellForRowAtIndexPath with the following error message:
I've placed NSLogs in various parts of the function to see where it crashes, but they all log successfully, so I'm a bit confused as to where it's going wrong.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
NSLog(#"cellForRowAtIndexPath has begun");
// parse data for the respective section's items
NSDictionary *currentSectionDictionary = _matchCenterData[indexPath.section];
NSArray *top3ArrayForSection = currentSectionDictionary[#"Top 3"];
// Cell defaults
static NSString *CellIdentifier = #"MatchCenterCell";
MatchCenterCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (!cell) {
// if no cell could be dequeued create a new one
cell = [[MatchCenterCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier];
}
cell.textLabel.text = [NSString stringWithFormat:#"Title"];
cell.textLabel.font = [UIFont systemFontOfSize:14];
tableView.separatorColor = [UIColor clearColor];
// if no results for that item
if (top3ArrayForSection.count-1 < 1) {
cell.detailTextLabel.text = [NSString stringWithFormat:#""];
[cell.imageView setImage:[UIImage imageNamed:#""]];
NSLog(#"no results for this item");
return cell;
}
// if results for that item found
else {
// Load images using background thread to avoid the laggy tableView
[cell.imageView setImage:[UIImage imageNamed:#"Placeholder.png"]];
NSLog(#"FOUND results for this item");
return cell;
}
}

Related

In UItableview, when I scroll table values are miss placing(values selecting from picker)

Here I am trying to do dynamic UITableViewCell with UIPickerView.
Step 1:
In Custom cell, I took 1 label and 1 UITextField.
Step 2:
used downpickerview library for data displaying and data fetching.
Step 3:
using below code I can able to select data, but after that, if I scroll UITableView data will be miss placing.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *MyIdentifier = #"Cell";
customCell *cell1=[tableView dequeueReusableCellWithIdentifier:MyIdentifier];
if (cell1 == nil)
{
cell1 = [[customCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:MyIdentifier];
}
cell1.productTitleLabel.text =[arrProductTitle objectAtIndex:indexPath.row];
self.downPicker = [[DownPicker alloc] initWithTextField:cell1.productvalueTextfield withData:arrProductVal];
[self.downPicker addTarget:self action:#selector(measurementSelected:) forControlEvents:UIControlEventValueChanged];
[cell1.contentView addSubview:self.downPicker];
return cell1;
}
Please help me on this.
- (void)viewDidLoad {
[super viewDidLoad];
self.navigationItem.title = #"DETAILS";
_dict = [[NSMutableDictionary alloc]init];
arrProductTitle = [[NSMutableArray alloc]initWithObjects:#"title0",#"title1",#"title2",#"title3",#"title4",#"title5",#"title6",#"title7",#"title8",#"title9",#"title10",#"title11",#"title12",#"title13",#"title14",#"title15", nil];
arrProductVal = [[NSMutableArray alloc]initWithObjects:#"0",#"1",#"2",#"3",#"4",#"5",nil];}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
return arrProductTitle.count-1;}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
static NSString *MyIdentifier = #"Cell";
customCell *cell1=[tableView dequeueReusableCellWithIdentifier:MyIdentifier];
if (cell1 == nil)
{
cell1 = [[customCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:MyIdentifier];
}
cell1.productTitleLabel.text =[arrProductTitle objectAtIndex:indexPath.row];
NSLog(#"%#",_dict);
NSLog(#"%#",[NSString stringWithFormat:#"%ld",(long)indexPath.row]);
if (_dict[[NSString stringWithFormat:#"%ld",(long)indexPath.row]]) {
cell1.productvalueTextfield.text =[_dict valueForKey:[NSString stringWithFormat:#"%ld",(long)indexPath.row]];
}
else {
cell1.productvalueTextfield.text = #"";
self.downPicker = [[DownPicker alloc] initWithTextField:cell1.productvalueTextfield withData:arrProductVal];
self.downPicker.tag = indexPath.row;
[self.downPicker addTarget:self action:#selector(measurementSelected:) forControlEvents:UIControlEventValueChanged];
[cell1.contentView addSubview:self.downPicker];
}
return cell1;}
-(void)measurementSelected:(id)dp {
NSString* selectedValue = [dp text];
NSString* selectedIndex = [NSString stringWithFormat:#"%ld",(long)[dp tag]];
[_dict setValue:[dp text] forKey:selectedIndex];
NSLog(#"_dict: %#",_dict);
NSLog(#"SELECTED TAG:::::::%ld",[dp tag]);
NSLog(#"SELECTED VALUE:::::::%#",selectedValue);
NSLog(#"SELECTED INDEX VALUEEEEEEEEEEE:::::::%ld",[dp selectedIndex]);}
https://github.com/gvniosdev/Dynamic-UItableview-with-Picker-Selection
UITableViewCell values are misplacing because you haven't set the data for other cells, You need to store the values in an array and just update the values from there and it will work. :)
Something is not right with your code:
cell1.productTitleLabel.text =[arrProductTitle objectAtIndex:indexPath.row];
self.downPicker = [[DownPicker alloc] initWithTextField:cell1.productvalueTextfield withData:arrProductVal];
[self.downPicker addTarget:self action:#selector(measurementSelected:) forControlEvents:UIControlEventValueChanged];
[cell1.contentView addSubview:self.downPicker];
Bear in mind that in iOS, a tableView will reuse its cells. So when you scroll your UITableView, it will reuse the old cells which were created before, and
[cell1.contentView addSubview:self.downPicker]; Will be executed everytime a cell is reused, as a result, you will end up having many downPicker objects in one cell.

Previous cell not disappearing in UITableView

I have a table which initially has no data, and shows a UITableViewCell with a message saying the user has no data to show.
I have a method that gets called on viewDidLoad and reloads the table data. When this happens, the first table cell is still shown, and the remaining cells are below it with proper data. The first cell still shows the empty data message. Is there anything i'm missing here? I have my cellForRowAtIndexPath method below.
- (UITableViewCell *)cellForRowAtIndexPath:(NSIndexPath *)indexPath tableView:(UITableView *)tableView {
static NSString *CellIdentifier = #"TableViewCell";
static NSString *CellIdentifierEmpty = #"TableViewCellEmpty";
if ([[self.fetchedResultsController.sections objectAtIndex:indexPath.section] numberOfObjects] == 0) {
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifierEmpty];
if(cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifierEmpty];
cell.backgroundColor = [UIColor whiteColor];
cell.textLabel.textColor = [UIColor blackColor];
cell.detailTextLabel.numberOfLines = 0;
cell.detailTextLabel.lineBreakMode = NSLineBreakByWordWrapping;
cell.detailTextLabel.textColor = [UIColor blackColor];
cell.tag = -1;
}
return [self setTextForEmptyCell:cell];
}
MyObject *object = [self.fetchedResultsController objectAtIndexPath:indexPath];
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (!cell) {
cell = // Set values
}
// Set cell details
return cell;
}
Found the issue. It was a threading issue while grabbing data from the server using AFNetworking. Check all the blocks where a new thread is being used!

Update UItableView in viewWillAppear?

I have problem with my UItableview that when I use viewWillAppear for retrieving updated data each time table is viewed this gives me exception .
Please can any one help me on how to refresh tableview in viewWillAppear so that it will be updated before CellforrowAtindexPath is called ?
Here is my code for viewWillAppear:
-(void)viewWillAppear:(BOOL)animated
{
[super viewWillAppear:YES];
[self.tableView reloadData];//table was filled in viewDidLoad.
}
Here is CellforrowAtindexPath` : (The exception is on mainArray which means that there is conflict in updating table through viewWillAppear and calling cell ):
//Methods to retrive all selected words , thier sound and picture for the mother
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifer = #"Cell";
UITableViewCell *cell= [ tableView dequeueReusableCellWithIdentifier:CellIdentifer];
cell.textLabel.font = [UIFont systemFontOfSize:17.0];
if (cell == nil)
{
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault
reuseIdentifier:CellIdentifer];
}
cell.textLabel.font = [UIFont systemFontOfSize:17.0];
UILabel *label ;
label=(UILabel *)[cell viewWithTag:1];
NSString *value = [[self.mainArray objectAtIndex:indexPath.section] objectAtIndex:indexPath.row];
label.text = value;
label.textAlignment=NSTextAlignmentRight;
label.autoresizingMask = UIViewAutoresizingFlexibleRightMargin;
//self.tableView.separatorColor = [UIColor blackColor];TRY TO SEPARETE BETEWEEN LINES OF TABLE
UIButton *butt =(UIButton *)[cell viewWithTag:2];
NSString *value2 = [[self.mainArray2 objectAtIndex:indexPath.section] objectAtIndex:indexPath.row];
butt.restorationIdentifier= value2;
[butt addTarget:self action:#selector(play:) forControlEvents:UIControlEventTouchUpInside];
NSString *value3 = [[self.mainArray3 objectAtIndex:indexPath.section] objectAtIndex:indexPath.row];
cell.imageView.image = [UIImage imageNamed:value3];
label.autoresizingMask = UIViewAutoresizingFlexibleRightMargin;
return cell;
}
(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section;{
return self.mainArray.count;
}
if count is 0, table view won't call the other functions.

why my uitableview cell row is repeating subtitles

In my uitableview I have two sections
first is fetched from core data
other is added through textfield which is stored in NSMutableArray(otherFriends)
here is my code
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
if([otherFriends count]>0)
{
return 2;
}
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)sectionIndex
{
if(otherFriends == 0)
{
return [[[[self fetchedResultsController]sections]objectAtIndex:0]numberOfObjects];
}
return [otherFriends count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"newGroupCell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil)
{
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier];
}
cell.textLabel.font = [UIFont fontWithName:#"Helvetica-Light" size:20.0];
cell.textLabel.backgroundColor = [UIColor colorWithWhite:1.0f alpha:0.0f];
if(indexPath.section == 0)
{
user = [[self fetchedResultsController] objectAtIndexPath:indexPath];
cell.textLabel.text = user.name;
cell.detailTextLabel.text = user.primaryResource.status;
[cell.imageView setFrame:CGRectMake(0, 0, 50, 50)];
[cell.imageView.layer setMasksToBounds:YES];
if (user.photo != nil)
{
cell.imageView.image = user.photo;
}
else
{
cell.imageView.image = [UIImage imageNamed:#"defaultPerson"];
}
}
else
{
cell.textLabel.text = [otherFriends objectAtIndex:indexPath.row];
cell.imageView.image = [UIImage imageNamed:#"defaultPerson"];
}
return cell;
}
First section also have subtitles but second section cells have no subtitles
when I am adding new friend it works fine till all rows are visible, but when adding new friend and that row is not visible and to view that row I must scroll, then this row show the subtitle of first row in section 0(the very first cell) and its repeating in section 1 third row. Only subtitle is repeating but the main text is not.
For several hours I am trying to figure out this but no luck.
This is because in your else branch you are not setting the cell.detailTextLabel.text.
When a cell gets recycled, the old detailTextLabel stays there. You need to set all properties of the cell in both branches of the conditionsl, on the chance that it has been recycled.
if(indexPath.section == 0)
{
user = [[self fetchedResultsController] objectAtIndexPath:indexPath];
cell.textLabel.text = user.name;
cell.detailTextLabel.text = user.primaryResource.status;
[cell.imageView setFrame:CGRectMake(0, 0, 50, 50)];
[cell.imageView.layer setMasksToBounds:YES];
if (user.photo != nil)
{
cell.imageView.image = user.photo;
}
else
{
cell.imageView.image = [UIImage imageNamed:#"defaultPerson"];
}
}
else
{
cell.textLabel.text = [otherFriends objectAtIndex:indexPath.row];
cell.imageView.image = [UIImage imageNamed:#"defaultPerson"];
// ADDED
cell.detailTextLabel.text = #"";
// You may also need to adjust the frame of the cell.imageView
// because it could have been recycled.
}
Cells get reused. Since you are using the same cells for both sections, you must be sure to set/reset the same set of cell properties in all conditions.
The problem is when the cell is used for section 1, you don't reset the detailTextLabel. In your else block, add:
cell.detailTextLabel.text = nil;
This ensures that in all cases you are setting the textLabel, detailTextLabel, and the imageView properties for the reused cells.

UITableView appears with empty cells

I have a problem displaying UITableView: Some cells are empty, and content in cells becomes visible only after scrolling (if empty cell scrolls out of the screen and then goes back). I Can't understand what might be the problem.
Here is my code:
- (void)viewDidLoad
{
[super viewDidLoad];
[self updateFilesList];
[tableView reloadData];
}
- (void) viewDidAppear:(BOOL)animated
{
animated = YES;
[self updateFilesList];
[self.tableView reloadData];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
[self.filesList retain];
NSString *title = [self.filesList objectAtIndex:indexPath.row];
title = [title stringByDeletingPathExtension];
title = [title lastPathComponent];
if (title.length >33) {
title = [title substringFromIndex:33];
}
static NSString *CellIdentifier = #"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
[cell.imageView setImage:[UIImage imageNamed:#"oie_png-1.png"]];
cell.textLabel.text = title;
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
}
return cell;
}
Thank you in advance for your suggestions!
Well, you have the cell customization code that happens before you create the cell.
Change it like this :
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
[self.filesList retain];
NSString *title = [self.filesList objectAtIndex:indexPath.row];
title = [title stringByDeletingPathExtension];
title = [title lastPathComponent];
if (title.length >33) {
title = [title substringFromIndex:33];
}
static NSString *CellIdentifier = #"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
// This part creates the cell for the firs time
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
}
// This part customizes the cells
[cell.imageView setImage:[UIImage imageNamed:#"oie_png-1.png"]];
cell.textLabel.text = title;
return cell;
}
The problem is that you make
[cell.imageView setImage:[UIImage imageNamed:#"oie_png-1.png"]];
cell.textLabel.text = title;
before
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
}
change the order. What is happening is that the first time you execute the table View you never do the title before the alloc. When you reuse cell it works because the cell != nil
You need to put these lines
[cell.imageView setImage:[UIImage imageNamed:#"oie_png-1.png"]];
cell.textLabel.text = title;
after the if(){...} condition.
In the first pass cells are nil. Put those lines before the if does nothing. This is why you see empty cells.
A simple question
Why do you call [self.filesList retain] ?
Hope it helps.

Resources