开发者

Is this efficient for a tableview?

I am just trying to clarify that this is the easiest and most efficient way to make the array of titles for the cells in my tableview.

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {

contentArray = [[NSArray arrayWithObjects:@"Hi1",开发者_如何学C @"Hi2", @"Hi3", @"Hi4", @"Hi5", @"Hi6", @"Hi7", nil] retain];
return [contentArray count];

[contentArray release];
}

Should I be doing this another way or this efficient and the best it can be? Because currently it is a bit laggy and I would like to make it a bit faster.

Thanks!


Calls to the method tableView:numberOfRowsInSection: can be triggered by different events. You are creating the array on each call, which increases the lag. Try this instead:

// .h file
@property (nonatomic, retain) NSArray *contentArray;

// .m file
@synthesize contentArray=_contentArray;

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return [self.contentArray count];
}

-(void) viewDidLoad {
  self.contentArray = [NSArray arrayWithObjects:@"Hi1", @"Hi2", @"Hi3", @"Hi4", @"Hi5", @"Hi6", @"Hi7", nil];
  [super viewDidLoad];
}
-(void) dealloc {
    [_contentArray release]
    [super dealloc];
}


What you do in this method has pretty much no influence at all on the scrolling performance of your table view because it is only going to be called a very limited number of times (unless your table contains hundreds or thousands of sections).

That said, your code contains many errors. Firstly, the line below return is never executed so you have a memory leak. Secondly, you should create the array outside the method and use a property to store it. But that has less to do with performance and more with good coding style. After all, you will need the contents of the array in multiple methods.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜