UITableView最后一行的文字会变化,求解

ithinco 发布于 2013年08月03日 | 更新于 2013年08月03日
无人欣赏。

问题描述请看视频,注意最后一行,用didSelectedRowAtIndex查选中行的文字,输出是正确的,但显示的却不是row 0或者row 1的文字,谢谢。

视频地址

代码:

#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView
*)tableView {
    // Return the number of sections.
    return 1; }

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    // Return the number of rows in the section.
    return [[[JINGMicroBlogStore sharedStore]allMicroBlogs]count]; }

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath
*)indexPath {
    static NSString *CellIdentifier = @"UITableViewCellDefault";
    UITableViewCell *cell=[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (!cell) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
        textView=[[UILabel alloc]initWithFrame:CGRectZero];
        [textView setTextColor:[UIColor blackColor]];
        [textView setFont:[UIFont fontWithName:@"Helvetica" size:12.0f]];
        [textView setBackgroundColor:[UIColor clearColor]];
        [textView setTag:1];
        //[textView setEditable:NO];
        textView.numberOfLines=0;
        [[cell contentView]addSubview:textView];
        NSLog(@"%d",[indexPath row]);
    }

    NSString *statue=[[[[JINGMicroBlogStore sharedStore]allMicroBlogs]objectAtIndex:[indexPath row]]statueText];
    CGSize constraint=CGSizeMake(320-(10*2),
99999.0f);
    CGSize size=[statue sizeWithFont:[UIFont systemFontOfSize:12.0f] constrainedToSize:constraint lineBreakMode:NSLineBreakByWordWrapping];
    [textView setText:statue];
    [textView setFrame:CGRectMake(10, 10, 320-(10*2),MAX(size.height,
44.0f))];

    return cell; }


-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath
*)indexPath{
    NSString *statue=[[[[JINGMicroBlogStore sharedStore]allMicroBlogs]objectAtIndex:[indexPath row]]statueText];
    //[[[statuses objectAtIndex:[indexPath row]] objectForKey:@"retweeted_status"]objectForKey:@"text"];
    CGSize constraint = CGSizeMake(320 - (10 * 2), 20000.0f);

    CGSize size = [statue sizeWithFont:[UIFont systemFontOfSize:12] constrainedToSize:constraint lineBreakMode:NSLineBreakByWordWrapping];

    CGFloat height = MAX(size.height, 44.0f);

    return height + (10 * 2); }

Update: 解决方法是cell用initWithFrame

共1条回复
tinyfool 回复于 2013年08月03日

你的代码明显有一个问题,就是cell如果是alloc出来的,那么textView是很明确的,但是如果是dequeueReusableCellWithIdentifier出来的话,textView没有明确的赋值,应该指向这个变量上次使用的值,这样当然会混乱。

登录 或者 注册
相关帖子