English 中文(简体)
Problem with cellForRowAtIndexPath and NSBundle mainBundle
原标题:

have an idea to solve this problem?

This is the correct and wrong way respectly:
alt text alt text

This is working code:

- (UITableViewCell *)[...] cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    @try {
        newsRow = ((NewsRowController *)[tableView dequeueReusableCellWithIdentifier:@"cell"]);
        if (newsRow == nil) {
            if ( [[Device GetModel] isEqualToString:@"iPad Simulator"] || 
                 [[Device GetModel] isEqualToString:@"iPad"])
                [[NSBundle mainBundle] loadNibNamed:@"NewsRow_ipad" owner:self options:nil];
            else [[NSBundle mainBundle] loadNibNamed:@"NewsRow" owner:self options:nil];

            if ([tableArray count] > 0)
                [newsRow setData:[tableArray objectAtIndex:indexPath.row]];
        }       
    }
    @catch (NSException * e) {
        NSLog(@"a!!!!!");
    }
    return newsRow;
}

...but because in my 3g device it s slowly when i scroll table up/down i changed the code in this way, inserting Device check in viewDidLoad and pass it in cellForRowAtIndexPath:

NSArray *cRow;
@property (nonatomic, retain) NSArray *cRow;
[...]
@syntetize cRow;

- (void)viewDidLoad {
[...]
    if ( [[Device GetModel] isEqualToString:@"iPad Simulator"] 
        || 
        [[Device GetModel] isEqualToString:@"iPad"])
        cRow  = [[[NSBundle mainBundle] loadNibNamed:@"NewsRow_ipad" owner:self options:nil] retain];
        else cRow = [[[NSBundle mainBundle] loadNibNamed:@"NewsRow" owner:self options:nil] retain];
[...]
}

- (UITableViewCell *)[...] cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    @try {
        newsRow = ((NewsRowController *)[tableView dequeueReusableCellWithIdentifier:@"cell"]);
        if (newsRow == nil) {

            //
            // ADDED CODE.
            //
            newsRow = [cRow objectAtIndex:0];    
            //

            if ([tableArray count] > 0)
                [newsRow setData:[tableArray objectAtIndex:indexPath.row]];
        }       
    }
    @catch (NSException * e) {
        NSLog(@"a!!!!!");
    }
    return newsRow;
}

Now the table show me only a row. If i scroll, the row change, but it show only one row...
alt text
What s the problem?

Any helps?

thanks,
A

最佳回答

In this case when you call dequeueReusableCellWithIdentifier: you will get nil back because there are no cells that can be dequeued. You need to load the nib each time you get a nil cell back otherwise you won t be adding any cells to the table after the first one.

If calling GetModel is what is slowing you could call that in viewDidLoad and store it for usage later on.

More than likely your table is not scrolling smoothly because of the amount of views you are drawing in each cell. If your background will always be a solid color you should make sure that all the views you are adding to the cell or opaque. If you need an image or a gradient background then you should look at drawing the cell all in one view. Apple has a good example of how to do this here: TableViewSuite. Specifically checkout the TimeZoneView file.

问题回答

暂无回答




相关问题
Asynchronous request to the server from background thread

I ve got the problem when I tried to do asynchronous requests to server from background thread. I ve never got results of those requests. Simple example which shows the problem: @protocol ...

objective-c: Calling a void function from another controller

i have a void, like -(void) doSomething in a specific controller. i can call it in this controller via [self doSomething], but i don t know how to call this void from another .m file. I want to call ...

ABPersonViewController Usage for displaying contact

Created a View based Project and added a contact to the AddressBook using ABAddressBookRef,ABRecordRef now i wanted to display the added contact ABPersonViewController is the method but how to use in ...

NSArray s, Primitive types and Boxing Oh My!

I m pretty new to the Objective-C world and I have a long history with .net/C# so naturally I m inclined to use my C# wits. Now here s the question: I feel really inclined to create some type of ...

NSUndoManager and runModalForWindow:

I have a simple Core Data app which displays a list of entities in the main window. To create or add new entities, I use a second modal window with a separate managed object context so changes can be ...

NSMutableArray values becoming "invalid"

I m trying to show a database information in a tableview and then the detailed information in a view my problem is as follow: I created a NSMutableArray: NSMutableArray *myArray = [[NSMutableArray ...

iPhone numberpad with decimal point

I am writing an iPhone application which requires the user to enter several values that may contain a decimal point (currency values, percentages etc.). The number of decimal places in the values ...

热门标签