How to Fix Crash When Tap to Select Row After Scrolling The Tableview

How can I fix crash when tap to select row after scrolling the tableview?

Because you are using reusable cells when you try to select a cell that is not in the screen anymore the app will crash as the cell is no long exist in memory, try this:

if let lastCell = self.diceFaceTable.cellForRowAtIndexPath(lastIndexPath) as! TableViewCell{
lastCell.checkImg.image = UIImage(named: "uncheck")
}
//update the data set from the table view with the change in the icon
//for the old and the new cell

This code will update the check box if the cell is currently in the screen. If it is not currently on the screen when you get the cell to reused (dequeuereusablecellwithidentifier) you should set it properly before display. To do so you will need to update the data set of the table view to contain the change.

uitableview crashed when scrolling

try this,

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
FSParallaxTableViewCell *cell = nil;
static NSString *cellIdentifier = nil;
// init expanded cell
if (indexPath.row >= [rssOutputData count])
cellIdentifier = @"ExpandedCellIdentifier";
}
// init expanding cell
else {
cellIdentifier = @"Cell";
}
cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier forIndexPath:indexPath];
if (cell == nil) {
cell = [[FSParallaxTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];
cell.cellImageView.image = [UIImage imageNamed:@"placeholder.png"];
}
if (indexPath.row >= [rssOutputData count]){
cell.playIconBig = [UIButton buttonWithType:UIButtonTypeRoundedRect];
cell.playIconBig.tag= indexPath.row-1;
[cell.playIconBig addTarget:self action:@selector(play:) forControlEvents:UIControlEventTouchDown];
cell.playIconBig.frame = CGRectMake(25, 5, 25, 25);
[cell.playIconBig setBackgroundImage:[UIImage imageNamed:@"play_expand.png"] forState:UIControlStateNormal];
[cell.contentView addSubview:cell.playIconBig];
}else {
[cell.cellImageView sd_setImageWithURL:[NSURL URLWithString:[[rssOutputData objectAtIndex:indexPath.row]xmllink]] placeholderImage:[UIImage imageNamed:@"placeholder"] options:indexPath.row == 0 ? SDWebImageRefreshCached : 0];
cell.clipsToBounds = YES;

//[cell.contentView addSubview:arrowImg];

cell.song.text = [[rssOutputData objectAtIndex:indexPath.row]xmltitle];
cell.artist.text = [NSString stringWithFormat:@"By %@",[[rssOutputData objectAtIndex:indexPath.row]xmlsinger]];
cell.share.text = [[rssOutputData objectAtIndex:indexPath.row]xmllikes];
cell.download.text = [[rssOutputData objectAtIndex:indexPath.row]xmldownloads];
}
return cell;
}

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

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
// take expanded cell into account when returning number of rows
if (self.expandedIndexPath) {
return [rssOutputData count] + 1;
}
return [rssOutputData count];
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{

if ([indexPath isEqual:self.expandedIndexPath])
{
return 40;//height for expanded
} else {
return 140;//height for normal
}
}

UITableView scrolling crashes App

Okay so, if you followed the steps to reproduce I will now add the steps to solve this:

Steps to fix this problem

  1. Subclass a UIViewController. I called mine CombinationsViewController. In this controller add a property as an IBOutlet for the combinationsTableViewController from step 6 below.

  2. Don't forget to import the right stuff, and synthesize the table view controller, also release it in the dealloc method.

  3. In FirstView.xib change the File's Owner class to this latest subclass.

  4. Link it's combinationsViewController outlet to the Combinations Table View Controller in the FirstView.xib made in step 7 below.

  5. Open the TabBarController in your MainWindow.xib select the first tab, and in the Identity Inspector change the class to the latest subclass (CombinationsViewController).

That makes the table populate normally and scroll stuff.

Now I'm going to move on finally and get some custom table view cell stuff happening and actually make my app do stuff.

Enumerated Steps to reproduce as a reference to the fix:

  1. Made a new "Tab Bar Application" in Xcode.

  2. Opened the Tab View Controller, dragged the prefab view for the first tab out into the MainWindow.xib

  3. Made a new view based xib called FirstView.xib as an analogue to the given SecondView.xib, and put that prefab view into this xib.

  4. Linked the view to the File's Owner's view outlet.

  5. I modified the view to contain two UITextFields for inputs, and a UITableView for output.

  6. I subclassed the UITableViewController as CombinationsTableView wherein I populated an NSArray property named combinations with 11 strings, and then added the cell.text = [combinations objectAtIndex:indexPath.row]; code where there had only been the comment about setting up the cell.

  7. In FirstView.xib I added a Table View Controller and set it's class name to match the name of this new subclass, and control-dragged the Table View in my view onto this Combinations Table View Controller twice. Once linking the dataSource and once the delegate.

At this point the table does render with data, but the scrolling breaks. This is because the CombinationsTableView isn't retained anywhere. And that's very unclear to a first time IB user. So you need to apply the fix listed above.

The first person to summarize this in their answer get the correct answer check mark. E.G. Make a viewController subclass that is the file owner of FirstView.xib and contains an retained IBOutlet you can link to your table view controller in the same xib file.

Why does data returned from coredata become nil after tableview scroll in swift?

I found out eventually that it was actually a threading(concurrence) problem.

Basically, in the getSuperTrainerData function, a bunch of objects were supposed to be created. Given that they returned nill all the time, the ViewController would of course refuse to create the rows.. however, entering the same view twice would have given the app time to store and cache the objects returned from a network call.

I make a call to Utility.backgroundThread which is just a wrapper for dispatchQueue. This means that networkService was placed inside a background thread. But inside networkService there is a call to urlSession. Call to servers create their own background threads, so even though I called my server call throuhg a background thread, it created its own background thread and never returned to the main call.

The solution that I used was to just make a server call, and place the background-object-creation call in the completion handler like so:

    self.networkService.getSuperTrainerData(student: student) { (complet, returnDics, errorMessage) -> Void in
if(complet) {
DispatchQueue.global(qos: DispatchQoS.userInitiated.qosClass ).async {
self.removeSuperTrainerData();
self.createSuperTrainerData(dics: returnDics!);

DispatchQueue.main.async( execute: {
print("main queue without completion")
self.networkService.coreDataHandler.saveContext();
self.topics = Topic.getTopics(student: self.student!, context:self.networkService.coreDataHandler.context!)!;
SwiftSpinner.hide();
self.tableView.reloadData();
})
}

} else {
Utility.showAlertView(title: "LOGIN_FAILED_TITLE".localized, message: errorMessage);
}
}

Hope this helps someone :)

Crash on switching segment control

From this case i came to understand you are using same UITableView for three segments also i came to know your call tableView.reloadData() when you change segment the crash happen because of one segement have more data and other don't have when scroll tableView. tableView's visble index positions not available in other segement's datasource because of that only you get crash and solution for this you need to scroll to the 0th index if count greater than 0 then reload tableView

when click on other segement before changing value to be populated scroll back to 0th index

 if listArray.count > 0{
let index = IndexPath(row: 0, section: 0)
self.tableView.scrollToRow(at: index, at: .bottom, animated: false)
}

then change the value for clickedSegement to listArray

then reload tableview

self.tableView.reloadData()

Hope this will help you



Related Topics



Leave a reply



Submit