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
Subclass a
UIViewController
. I called mineCombinationsViewController
. In this controller add a property as an IBOutlet for thecombinations
Table
ViewController
from step 6 below.Don't forget to import the right stuff, and synthesize the table view controller, also release it in the
dealloc
method.In
FirstView.xib
change theFile's Owner
class to this latest subclass.Link it's combinationsViewController outlet to the
Combinations Table View Controller
in theFirstView.xib
made in step 7 below.Open the
TabBarController
in yourMainWindow.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:
Made a new "Tab Bar Application" in Xcode.
Opened the
Tab View Controller
, dragged the prefab view for the first tab out into theMainWindow.xib
Made a new view based xib called
FirstView.xib
as an analogue to the givenSecondView.xib
, and put that prefab view into this xib.Linked the view to the
File's Owner
'sview
outlet.I modified the view to contain two
UITextField
s for inputs, and aUITableView
for output.I subclassed the
UITableViewController
asCombinationsTableView
wherein I populated anNSArray
property namedcombinations
with 11 strings, and then added thecell.text = [combinations objectAtIndex:indexPath.row];
code where there had only been the comment about setting up the cell.In
FirstView.xib
I added aTable View Controller
and set it's class name to match the name of this new subclass, and control-dragged theTable View
in my view onto thisCombinations Table View Controller
twice. Once linking thedataSource
and once thedelegate
.
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
How to Use Pod in Notification Service Extension
How to Implement a "Rate Us" Feature in a Phonegap App
Stop Overscroll When Using "-Webkit-Overflow-Scrolling: Touch"
Cancel All Operations + Afnetworking 3.0
Problems with Layout of Some Rows in Swiftui List
Swift4: Callback Url Not Approved for This Client Application
Atos Does Not Symbolicate System Frameworks/Libraries Properly
Indexpathforrowatpoint Returns Nil Only for First Cell in a UItableview
Draw Polyline Using Google Maps in Custom View with Swift 3
iOS Add Button to Widget Extension
How to Dismiss a Modal Segue Then Perform Push Segue to New View Controller
Stick UItableview Header View to Top When Creating Header in Storyboard
How to Check Bitfields (Scnetworkreachabilityflags in Particular) for Flags in Swift
Will Download Resume After Closing My App in Background Mode
iOS UIlabel Autoshrink So Word Doesn't Truncate to Two Lines