tableViewの表示がおかしい

フォーラム(掲示板)ルール
フォーラム(掲示板)ルールはこちら  ※コードを貼り付ける場合は [code][/code] で囲って下さい。詳しくはこちら
TEAS TEA

tableViewの表示がおかしい

#1

投稿記事 by TEAS TEA » 14年前

index.pathが0でない所に「あああ」と表示されてしまいます。
スクロールすると「あああ」の位置がおかしくなってしまいます。
原因がわかりません・・・。
写真を添付させて頂きます。
お分かりになる方はいらっしゃいませんでしょうか?

又、myDataSourceの容量より1枠多めに表示しております。
これはindex.pathが0の時だけ別データを表示する為です。

コード:



------------------------------------------------------
ViewController1.m
------------------------------------------------------

#import "ViewController1.h"
#import "MyTableViewCell.h"

@implementation ViewController1
@synthesize url;

#pragma mark -
#pragma mark View lifecycle
/*
// UICOLOR http://iphone-tora.sakura.ne.jp/uicolor.html
- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath {
	if (indexPath.row == 0 || indexPath.row%2 == 0) {
		UIColor *color = [UIColor colorWithRed:1.0 green:0.98 blue:0.667 alpha:0.7];
		cell.backgroundColor = color;
	}
}
*/
- (void)viewDidLoad {
	
    [super viewDidLoad];
	
	self.navigationController.navigationBar.barStyle = UIBarStyleBlack;
	
	// タイトル
	//	self.title = @"RSS取得";
	
	parser = [[RSSParser alloc] init];
	[parser parseRSSData:[NSURL URLWithString:url]];
	//	NSLog( @"%@", rssURL);
	myDataSource = [[NSMutableArray alloc] init];
	
	//	myDataSource = [parser items];
	[myDataSource addObjectsFromArray:[parser items]];
	//	[myDataSource addObjectsFromArray:[parser items]];
	
}

#pragma mark -
#pragma mark Table view data source

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


// Customize the number of rows in the table view.
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return [myDataSource count]+1;
}


// Customize the appearance of table view cells.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
	
	static NSString *CellIdentifier = @"Cell";
    
	MyTableViewCell *cell = (MyTableViewCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[[MyTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
    }
	
	if( indexPath.row == 0 ){
		cell.textLabel.text = @"あああ";
	}else{
		[cell setLabelText:[myDataSource objectAtIndex:indexPath.row-1]];
	}
	return cell;
	
}
#pragma mark -
#pragma mark Table view delegate

// セルが選択されたら
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    /*
	if( indexPath.row != 0 ){
		DetailViewController *detailViewController = [[DetailViewController alloc] init];
		detailViewController.item = [myDataSource objectAtIndex:indexPath.row-1];
		[[self navigationController] pushViewController:detailViewController animated:YES];
		[detailViewController release];
	}
	 */
	NSLog( @"%d", indexPath.row );
	
}


#pragma mark -
#pragma mark Memory management

- (void)didReceiveMemoryWarning {
    // Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];
    
    // Relinquish ownership any cached data, images, etc that aren't in use.
}

- (void)viewDidUnload {
    // Relinquish ownership of anything that can be recreated in viewDidLoad or on demand.
    // For example: self.myOutlet = nil;
}


------------------------------------------------------
MyTableViewCell.m
------------------------------------------------------
#import "MyTableViewCell.h"

@implementation MyTableViewCell

- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
    if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
		titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, self.contentView.frame.size.height*0.5, self.contentView.frame.size.width, self.contentView.frame.size.height*0.5)];
		titleLabel.font = [UIFont systemFontOfSize:12];
		titleLabel.backgroundColor = [UIColor clearColor];
		
		pubDateLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 5, self.contentView.frame.size.width-20, self.contentView.frame.size.height*0.4)];
		pubDateLabel.font = [UIFont systemFontOfSize:12];
		pubDateLabel.backgroundColor = [UIColor clearColor];
		
		[self.contentView addSubview:titleLabel];
		[self.contentView addSubview:pubDateLabel];
		
//		self.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
//		[self.imageView setImage:[UIImage imageNamed:@"back.png"]];
    }
    return self;	
}


- (void)setSelected:(BOOL)selected animated:(BOOL)animated {

    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}
 
- (void)setLabelText:(Item *)item {
	titleLabel.text = item.title;
	pubDateLabel.text = item.pubDate;
}


- (void)dealloc {
	[titleLabel release];
	[pubDateLabel release];
    [super dealloc];	
}


@end

TEAS TEA

Re: tableViewの表示がおかしい

#2

投稿記事 by TEAS TEA » 14年前

すみません。画像を貼り忘れました。
画像は下記になります。
画像

アバター
Justy
副管理人
記事: 122
登録日時: 14年前
住所: 神奈川県

Re: tableViewの表示がおかしい

#3

投稿記事 by Justy » 14年前

cellは再利用されるのでそのあたりが原因なのかもしれません。
indexPath.row == 0ではない時、cell.textLabel.textに空文字列を入れてみたらどうなりますか?

TEAS TEA

Re: tableViewの表示がおかしい

#4

投稿記事 by TEAS TEA » 14年前

またまたJusty様ありがとうございます。
修正しました所、解決できました。
いつも本当にありがとうございます。

閉鎖

“C言語何でも質問掲示板” へ戻る