简体   繁体   中英

Problem while scrolling the UITableView with custom cell that contains a UItextview

I have a table view with custom cell that contains UITextView, UILabel and a UIButton.

The problem is,

When the table is loaded first time, 2 cells are shown and are fine. but any of the cell get scrolled out of the screen while scrolling the table, app suddenly get terminated without showing any exception error to Console.

this is my code,

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

NSLog(@"In tableView:Cell");    
static NSString *CustomCellIdentifier = @"CustomCellIdentifier ";

CourtsFavoriteCustomCell* cell = (CourtsFavoriteCustomCell *)[tableView dequeueReusableCellWithIdentifier: CustomCellIdentifier];

if( cell == nil)
    {
    NSArray *nib = [[NSBundle mainBundle] loadNibNamed:@"CourtsFavoriteCustomCell"
                                                    owner:self options:nil];

    for (id oneObject in nib) 
        if ([oneObject isKindOfClass:[CourtsFavoriteCustomCell class]])
        cell = (CourtsFavoriteCustomCell *)oneObject;

//*********** Creating button inside each TableView cell..

        UIButton *mapButton = [UIButton buttonWithType:UIButtonTypeRoundedRect] ;

        [mapButton setFrame:CGRectMake(120.0f, 157.0f, 72.0f, 32.0f)] ;
        //btnDetail.backgroundColor = [UIColor grayColor];
        [mapButton setTitle:@"Map" forState:UIControlStateNormal];
        [mapButton setTitleColor: [UIColor redColor] forState: UIControlStateNormal];
        //[mapButton.titleLabel setFont:[UIFont fontWithName:@"Verdana-Bold" size:12]];
        [mapButton setBackgroundColor:[UIColor clearColor]];
        [mapButton sizeThatFits:mapButton.frame.size];
        [cell addSubview:mapButton];
        [mapButton addTarget:self 
                      action:@selector(cellMapButtontapped:)
            forControlEvents:UIControlEventTouchUpInside];
        forState:UIControlStateNormal];

    }
NSLog(@"App get terminated after showing this log message to console when scrolling tableview");



    cell.name.text         = favCourtName;  // name is a UIText view and if comment this line of code, every thing works fine
    cell.address.text      = favCourtAddress;   


    cell.accessoryType          = UITableViewCellAccessoryDetailDisclosureButton; 

     return cell;


}

I am using UITextView (Cell.name in the code)in my custom cell. When I tried commenting the UITextView code out, then everything works fine.

Please help me:) thanx..

From the Apple Docs;

Important: You should not embed UIWebView or UITableView objects in UIScrollView objects. If you do so, unexpected behavior can result because touch events for the two objects can be mixed up and wrongly handled.

UITableView and UITextView are both subclasses of UIScrollView.

Once you get your memory problems sorted out you will still get unexpected behavior. You should probably rethink this design in favor of one that is supported.

You don't provide enough info to help further. What error message do you get on the console?

Nothing to do with your problem: It is atypical to name properties with an uppercase first character (ie cell.Name ). The Name property on your cell should be lower case (ie name ).

@All, Thanx for all of yours answers.. :) I solved the problem . It was not with UITextView.

And why I am writing here because, If anybody else comes here with the same problem it may help.

Problem was actually very simple, but took my 2 days to find out because it did not show any exceptions.

What I did was, just added the 'retain' to my 'favCourtName' in my ViewDidLoad method. Its like this,

NSString * favCourtName = [[[NSString alloc] initWithFormat:@"myCourt"] retain];

The word 'retain' does the magic. Thank you

favCourtName is released somewhere and is it allocated only single time? or on every cell it will be initialized?

cell.Name.text         = favCourtName;

Second thing about the performance, if you use the below code, it will slow down while scrolling as it is being loaded from Nib file, instead you can work using initWithStyle: .

NSArray *nib = [[NSBundle mainBundle] loadNibNamed:@"CourtsFavoriteCustomCell"
                                                owner:self options:nil];

for (id oneObject in nib) 
    if ([oneObject isKindOfClass:[CourtsFavoriteCustomCell class]])
    cell = (CourtsFavoriteCustomCell *)oneObject;

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