2011-01-12 92 views
1

你好我的表視圖工作overlaping,滾動iPhone:自定義單元格與對方

時,這是我的代碼

我的表視圖第一個自定義單元格是在乘坐其他細胞

#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]; 
} 
+0

喜tweetsTableView加載很慢,滾動表視圖時速度很慢,你能建議我 – 2011-01-26 04:30:36

回答

3

不要再使用該電池

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:nil]; 

所有最優秀的。

+0

嗨Warrior,這個工作對我來說,謝謝 – 2011-01-12 14:14:54

0

我認爲細胞的高度不合適。

檢查代碼。

+0

謝謝羅賓 – 2011-01-12 14:15:32

+0

喜tweetsTableView加載很慢,滾動表視圖非常緩慢滾動的時候,你可以建議我 – 2011-01-26 04:33:06

1

您一次又一次地將組件添加到可能重複使用的單元中。

您需要將創建添加標籤等添加到if (!cell) { }塊中,然後僅配置這些組件。

查找正確的UI元素的一種方法是在該塊中分配一個tag,另一種更優雅的方式是使用一個自定義子類以及這些元素的屬性。

編輯:並添加它們後發佈這些意見 - 或者你只是泄漏它們。

0

您可以使用NSString * identifier = @ [NSString stringWithFormat:@「%d」,indexPath.row]作爲單元標識符。它將細胞與其他細胞區分開來。