iPhone: пользовательские ячейки перекрываются друг с другом - PullRequest
1 голос
/ 12 января 2011

привет, я работаю над табличным представлением,

моя первая пользовательская ячейка табличного представления переходит другую ячейку при прокрутке

это мой код

#import UIKit/UIKit.h 

@interface MyTweetViewController : UIViewController<UITableViewDelegate,UITableViewDataSource> {

IBOutlet  UITableView *tweetsTableView;

NSMutableArray *tweetArray;

}

@property (nonatomic, retain) IBOutlet  UITableView *tweetsTableView;

@end

#import "MyTweetViewController.h"

@implementation MyTweetViewController

@synthesize  tweetsTableView;

- (void)viewDidLoad {

         tweetArray = [[NSMutableArray alloc] init];

    [tweetsTableView setBackgroundColor:[UIColor clearColor]];

    [super viewDidLoad];
}

#pragma mark -
#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 [tweetArray count];

}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath

{

return 80;

}
- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath

{

    [cell setBackgroundColor:[UIColor clearColor]];

}


 //Customize the appearance of table view cells.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

     static NSString *identifier = @"Cell";

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];


if(!cell) {


        cell = [[UITableViewCell alloc] initWithStyle:UITableViewStyleGrouped reuseIdentifier:identifier];

        cell.selectionStyle = UITableViewCellSelectionStyleNone;

    }

cell.accessoryType = UITableViewCellAccessoryNone;

UILabel * name  = [[UILabel alloc]initWithFrame:CGRectMake(72,3,242,15)];

name.text   = (NSString*)[(Tweet*)[tweetArray objectAtIndex:indexPath.row] userName];

[name setFont:[UIFont fontWithName:@"Helvetica" size:14]];

    name.textColor=[UIColor colorWithRed:250 green:250 blue:210 alpha:0.5];

    [name setBackgroundColor:[UIColor clearColor]];

    UILabel * tweetLabel  = [[UILabel alloc]initWithFrame:CGRectMake(72,20,242,60)];

    tweetLabel.text   = (NSString*)[(Tweet*)[tweetArray objectAtIndex:indexPath.row] tweet];

tweetLabel.numberOfLines = 3;

    tweetLabel.textColor=[UIColor colorWithRed:252 green:148 blue:31 alpha:1];

    [tweetLabel setFont:[UIFont fontWithName:@"Helvetica" size:12]];

    [tweetLabel setBackgroundColor:[UIColor clearColor]];

     NSLog(@" lblUserTweet : %@ ",name.text);

    UIImageView *myImage = [[UIImageView alloc]initWithFrame:CGRectMake(6,3,58,49)];

NSURL *url = [NSURL URLWithString:[(Tweet*)[tweetArray objectAtIndex:indexPath.row] image_url]];

NSData *data = [NSData dataWithContentsOfURL:url];

    [myImage setImage: [UIImage imageWithData:data]];

[cell.contentView addSubview:myImage];

    [cell.contentView addSubview:tweetLabel];

    [cell.contentView addSubview:name];


    return cell;

}

- (void)dealloc {

    [tweetsTableView release];

    [tweetArray release];

        [super dealloc];
}

Ответы [ 4 ]

3 голосов
/ 12 января 2011

Не используйте ячейку

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:nil];

Всего наилучшего.

1 голос
/ 12 января 2011

Вы добавляете компоненты в свою потенциально повторно используемую ячейку снова и снова.

Вам необходимо переместить создание, добавление надписей и т. Д. В блок if (!cell) { } и только потом настраивать эти компоненты.

Один из способов найти правильные элементы пользовательского интерфейса - найти их по tag, который вы назначаете в этом блоке, другой - более элегантный способ - использовать собственный подкласс UITableViewCell вместе со свойствами для этих элементов.

Редактировать: И освободить эти представления после добавления их - или вы просто потеряете их.

0 голосов
/ 09 августа 2011

вы можете использовать NSString * identifier = @ [NSString stringWithFormat: @ "% d", indexPath.row] в качестве идентификатора ячейки. Это отличает клетку от другой клетки.

0 голосов
/ 12 января 2011

Я думаю, что высота ячейки неправильная.

Проверьте код.

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...