通过cell.contentView将UIView添加到UITableViewCell [英] Adding a UIView to a UITableViewCell via cell.contentView
问题描述
我有一个名为GraphView的类,它扩展了UIView,基本上绘制了一个小的折线图。我需要在UITableView的每个部分顶部显示其中一个图形。所以我尝试在我的一个部分的顶部创建一个单独的单元格,然后在那个单元格上创建:
I have a class called GraphView that extends UIView that basically draws a small little line chart. I need one of these graphs at the top of each section in my UITableView. So I tried by creating a separate cell at the top of one of my sections and then on that cell I did:
[cell.contentView addSubview:graphView];
[graphView release];
但是当我向下滚动时,它就像图表是故障的,它会出现在沿着它的随机点UITableView的。任何人有想法或见解?有没有更好的方法将另一个UIView合并到我的UITableView的每个部分的顶部?
But when I scroll down, it's like the graph is glitchy and it shows up in random spots along the UITableView. Anyone have ideas or insight? Is there a better way to incorporate another UIView into the top of each section in my 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];
}
推荐答案
我已经解决了使用标签:
I've solved using tags:
- (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];
}
这篇关于通过cell.contentView将UIView添加到UITableViewCell的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!