简体   繁体   中英

TableView crashes

for some reason this UITableView keeps crashing and it seems like the UITableView is unable to display the secondsString for some reason but i have no idea why... please help me out here cause i have no idea what i could be doing wrong..... Thanks!!

#import "SettingsView.h"

@implementation SettingsView

- (void)viewWillAppear:(BOOL)animated {

   [super viewWillAppear:animated];
   transmissionSetup = [[TransmissionSetupViewController alloc]     initWithNibName:@"TransmissionSetupViewController" bundle:nil];
   transmissionSetup.modalTransitionStyle = UIModalTransitionStyleCoverVertical; 
   NSLog(@"%i", [self getMinutes]);
   [self setSeconds:10];
   secondsString = [NSString stringWithFormat:@"%i", [self getSeconds]]; 
   [self.tableView reloadData];
}

 #pragma mark Table view methods

// Customize the appearance of table view cells.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

   static NSString *CellIdentifier = @"Cell";

   UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
   if (cell == nil) {
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:CellIdentifier] autorelease];
    }

   if (indexPath.section == 0) {
       cell.textLabel.text =  @"Every:";
       cell.detailTextLabel.text = secondsString;
    }
    // Set up the cell...

    return cell;
}

@end

You initialize your secondString with autoreleased string - so it is not guaranteed that it will be valid outside current scope. You need to retain it upon initialization (and don't forget to release later)

I would advise you to have a look at objective-c properties for accessing ivars.

// try this
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

static NSString *CellIdentifier = @"Cell";

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
    cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
    cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier] autorelease];
}

if (indexPath.section == 0) {
    cell.textLabel.text =  @"Every:";
    cell.detailTextLabel.text = secondsString;
}
// Set up the cell...

return cell;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM