分享

IOS的TableViewCell会重叠的问题

 没原创_去搜索 2015-07-29

1. 如上图一个 UITableView 被重复插入两个图, 请看代码 


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
- (UITableViewCell *)tableView:(UITableView *)tableview cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *TableSampleIdentifier = @"TableSampleIdentifier";
    UITableViewCell *cell = [tableview dequeueReusableCellWithIdentifier:TableSampleIdentifier];
    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:TableSampleIdentifier];
    }
    cell.selectionStyle = UITableViewCellSeparatorStyleNone;
     
    NSUInteger row = [indexPath row];
    NSUInteger section = [indexPath section];
 
    horizontalLinesProperties=nil;
    anchorPropertiesArray=nil;
    yValuesArray = nil;
    xValuesArray = nil;
    xTitlesArray = nil;
    xProperty = nil;
    yProperty = nil;
    verticalLinesProperties = nil;
     
    NSArray *line1 = [[NSArray alloc]initWithObjects:
                      @"10000",@"21000",@"24000",@"11000",@"5000",@"2000",@"9000",@"4000",@"10000",@"17000",@"15000",@"11000",nil];
    NSArray *line2 = [[NSArray alloc]initWithObjects:
                      @"Jan",@"Feb",@"Mar",@"Apr",@"May",@"Jun",@"Jul",@"Aug",@"Sep",@"Oct",@"Nov", @"Dec",nil];
     
    horizontalLinesProperties=[NSDictionary dictionaryWithObjectsAndKeys:@"1,2",@"dotted", nil];
    verticalLinesProperties=[NSDictionary dictionaryWithObjectsAndKeys:@"1,2",@"dotted", nil];
     
    yValuesArray = line1;
    xValuesArray = line2;
    xTitlesArray = line2;
     
    mLineGraph=[[MIMLineGraph alloc]initWithFrame:CGRectMake(5, 5, 310, 150)];
    mLineGraph.delegate=self;
     
    mLineGraph.titleLabel.text = nil;
     
    mLineGraph.mbackgroundColor=[MIMColorClass colorWithComponent:@"1,1,1"];
    mLineGraph.anchorTypeArray=[NSArray arrayWithObjects:[NSNumber numberWithInt:NONE], nil];
    MIMColorClass *c1=[MIMColorClass colorWithComponent:@"0,169,249"];
    mLineGraph.lineColorArray=[NSArray arrayWithObjects:c1, nil];
    mLineGraph.xTitleStyle=X_TITLES_STYLE1;
     
    mLineGraph.leftMargin = -15;
    mLineGraph.rightMargin = 6;
    mLineGraph.topMargin = 10;
    mLineGraph.bottomMargin= -60;
     
    [mLineGraph drawMIMLineGraph];
 
    switch (section)
    {
        case 0:
            [cell.contentView addSubview:mLineGraph];
            break;
             
        case 2:
            [cell.contentView addSubview:mWallGraph];
            break;
             
        default:
            break;
    }
    return cell;
}
上面代码省掉了一段, 与那个 mLineGraph 差不多的 
1
mWallGraph

上面的图很明显被插入了两张图, 我通过 NSlog来打印 log 发现执行得并没有错误, 但是同一个 Cell 被插入两张图, 不知为何? 求解 

皮总皮总
发帖于 3年前
7回/4616阅

按票数排序  显示最新答案  共有7个答案 (最后回答: 3年前)

    0
  • 皮总
    擦, 像这样就可以了!  啥玩意  
    1
    2
    3
    4
    5
    static NSString *TableSampleIdentifier = @"TableSampleIdentifier";
        UITableViewCell *cell = [tableview dequeueReusableCellWithIdentifier:TableSampleIdentifier];
        if (cell == nil) {
            cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"myCell"];
        }
    评论(0)引用此答案| 举报 (2013-01-22 13:29)
  • 0
  • dake
    没看出来有两张图...解释一下..
    --- 共有 1 条评论 --- 
    • 皮总看坐标轴啊, 有一个图在背面 (3年前)  
    评论(1)引用此答案| 举报 (2013-01-22 12:08)
  • 0
  • dake
    不过问题可以出在cell的复用上...因为cell.contentView addSubview 的关系 ..所以用过的cell还会继续保持上一次add的图.......你懂的.....于是就重复了
    --- 共有 1 条评论 --- 
    • 皮总那我该怎么办呢 ? (3年前)  
    评论(1)引用此答案| 举报 (2013-01-22 12:12)
  • 0
  • ajavaloser
    皮总的分本来就不好拿,结果丫还把自己的问题给答了 
    --- 共有 3 条评论 --- 
    • 皮总回复 @wwwmmm : 擦, 还能这样 (3年前)  
    • wwwmmm回复 @皮总 : 我举报了 洗积分 (3年前)  
    • 皮总我的分向来好拿啊! (3年前)  
    评论(3)引用此答案| 举报 (2013-01-22 13:31)
  • 0
  • 李烈火

    引用来自“皮总”的答案

    擦, 像这样就可以了!  啥玩意  
    1
    2
    3
    4
    5
    static NSString *TableSampleIdentifier = @"TableSampleIdentifier";
        UITableViewCell *cell = [tableview dequeueReusableCellWithIdentifier:TableSampleIdentifier];
        if (cell == nil) {
            cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"myCell"];
        }
    我是没懂,答案如上
    评论(0)引用此答案| 举报 (2013-01-22 13:37)
  • 0
  • dake
    没分....我去..
    评论(0)引用此答案| 举报 (2013-01-22 13:39)
  • 0
  • blindcat
    我要积分,我不答题 

    本站是提供个人知识管理的网络存储空间,所有内容均由用户发布,不代表本站观点。请注意甄别内容中的联系方式、诱导购买等信息,谨防诈骗。如发现有害或侵权内容,请点击一键举报。
    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约

    类似文章 更多