Problem with cellForRowAtIndexPath and NSBundle mainBundle
have an idea to solve this problem?
This is the correct and wrong way respectly:
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 dequeueReus开发者_如何学PythonableCellWithIdentifier:@"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...
What's the problem?Any helps?
thanks,
AIn 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.
精彩评论