Cod*_*Guy 4 iphone uitableview ios
我有一个名为GraphView的类,它扩展了UIView,基本上绘制了一个小的折线图.我需要在UITableView的每个部分顶部显示其中一个图形.所以我试着在我的一个部分的顶部创建一个单独的单元格,然后在那个单元格上我做了:
[cell.contentView addSubview:graphView];
[graphView release];
Run Code Online (Sandbox Code Playgroud)
但是当我向下滚动时,它就像图表是故障的,它会沿着UITableView随机出现.任何人有想法或见解?有没有更好的方法将另一个UIView合并到我的UITableView中每个部分的顶部?
- (UITableViewCell *)tableView:(UITableView *)theTableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
//NSLog(@"cellForrowAtIndexPath");
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [theTableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:CellIdentifier] autorelease];
}
cell.selectionStyle = UITableViewCellSelectionStyleNone;
cell.textLabel.font = [UIFont systemFontOfSize:12];
cell.detailTextLabel.font = [UIFont systemFontOfSize:12];
NSString *str1, *str2;
if(indexPath.section == 0) {
if(indexPath.row == 0) {
str1 = @"";
str2 = @"";
S7GraphView *graphView = [[S7GraphView alloc] initWithFrame:CGRectMake(10,0,310,100)];
graphView.dataSource = self;
NSNumberFormatter *numberFormatter = [NSNumberFormatter new];
[numberFormatter setNumberStyle:NSNumberFormatterDecimalStyle];
[numberFormatter setMinimumFractionDigits:0];
[numberFormatter setMaximumFractionDigits:0];
graphView.yValuesFormatter = numberFormatter;
NSDateFormatter *dateFormatter = [NSDateFormatter new];
[dateFormatter setTimeStyle:NSDateFormatterNoStyle];
[dateFormatter setDateStyle:NSDateFormatterShortStyle];
graphView.xValuesFormatter = dateFormatter;
[dateFormatter release];
[numberFormatter release];
graphView.backgroundColor = [UIColor whiteColor];
graphView.drawAxisX = NO;
graphView.drawAxisY = YES;
graphView.drawGridX = NO;
graphView.drawGridY = YES;
graphView.xValuesColor = [UIColor blackColor];
graphView.yValuesColor = [UIColor blackColor];
graphView.gridXColor = [UIColor blackColor];
graphView.gridYColor = [UIColor blackColor];
graphView.drawInfo = NO;
graphView.info = @"Load";
graphView.infoColor = [UIColor whiteColor];
//When you need to update the data, make this call:
//[graphView reloadData];
//S7GraphView *graphView = [[S7GraphView alloc] initWithFrame:CGRectMake(10,0,320,300)];
//self.graphView.dataSource = self;
[cell.contentView addSubview:graphView];
[graphView release];
}
Run Code Online (Sandbox Code Playgroud)
Jor*_*ith 13
表视图的工作方式与您最初的预期略有不同.基本上,只有您看到的单元格实际上已加载到内存中.如果您考虑一下,这是有道理的 - 如果您的表中有10,000个项目,所有项目都加载到内存中,那么您的应用程序将耗尽内存并快速崩溃.
滚动时,您的单元格是实时创建的.令人困惑的部分:您的单元格不是从头开始创建的,而是iOS只需要一个刚刚离开屏幕并通过它发送的单元格
- (UITableViewCell *)tableView:(UITableView *)theTableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
Run Code Online (Sandbox Code Playgroud)
再次.因此,当您滚动时,当该单元格被进一步向下重用时,您添加到顶部单元格的子视图将不会被删除.简短回答:确保在上面的函数中删除/重置子视图,这样您将"重置"在表视图中进一步重用的任何已使用的单元格.
为什么iOS这样做?效率 - 基本上,创建tableview单元非常昂贵.找到宽度,颜色,绘制形状... +更多需要大量的CPU功率和时间.每次重复使用单元而不是从头开始更有效.
因此,只需在单元格创建功能中重置单元格的必要字段.这样,您将获得平滑且快速的滚动,但在重复使用单元格之前,将重置单元格的视图/属性.
一开始很困惑?对.但绝对是更好的方法.
int*_*dro 10
我用标签解决了:
- (UITableViewCell *)tableView:(UITableView *)theTableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [theTableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:CellIdentifier] autorelease];
}
else{
[[cell.contentView viewWithTag:500] removeFromSuperview];
}
...
graphView.tag = 500;
[cell.contentView addSubview:graphView];
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
31429 次 |
| 最近记录: |