カスタム UITableView を実装するアプリケーションを作成しています。私がこれを行った方法は次のとおりです。
私が作成した UINavigationController があります。この UINavigationController は、UITableViewDelegate と UITableViewDataSource を実装する UIViewController をプッシュします。この UIViewController の .xib を作成するため、UIViewController の 3 つのファイル: TestTableViewController.h、TestTableViewController.m、および TestTableViewController.xib が作成されます。
TestTableViewController.xib には、testTableView と呼ばれる IBOutlet UITableView にリンクされた UITableView のみが配置されており、この UITableView dataSource とデリゲートはファイルの所有者 (TestTableViewController) にリンクされています。
また、カスタム UITableViewCell を実装しました。UITableViewCell を配置し、UIView、UILabel、UIImageView、および UISwitch を挿入した TestCell.xib という名前の空の .xib を作成しました。次に、UITableViewCell から継承したクラスを次のように作成しました (TestCell.xib のビューを TestCell クラスとして定義し、識別子を TestCellIdentifier に設定しました)。
TestCell.h:
#import <UIKit/UIKit.h>
#define kTestHeight 40
@interface TestCell : UITableViewCell {
UIImageView *testImage;
UILabel *testLabel;
}
@property (nonatomic, retain) IBOutlet UIImageView *testImage;
@property (nonatomic, retain) IBOutlet UILabel *testLabel;
@end
TestCell.m:
#import "TestCell.h"
@implementation TestCell
@synthesize testImage, testLabel;
- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
if (self) {
// Initialization code
}
return self;
}
- (void)setSelected:(BOOL)selected animated:(BOOL)animated
{
[super setSelected:selected animated:animated];
// Configure the view for the selected state
}
- (void)dealloc
{
[self.testLabel release];
[self.testImage release];
[super dealloc];
}
@end
すべての TestTableViewController を次のように実装します。
TestTableViewController.h:
#import <UIKit/UIKit.h>
@interface TestTableViewController : UIViewController <UITableViewDelegate, UITableViewDataSource> {
NSArray *labels;
NSArray *images;
UITableView *testTableView; // this is the UITableView linked in the NIB
}
@property (nonatomic, retain) NSArray *labels, *images;
@property (nonatomic, retain) IBOutlet UITableView *testTableView;
@end
TestTableViewController.m:
#import "TestTableViewController.h"
#import "TestCell.h"
@implementation TestTableViewController
@synthesize labels, images;
@synthesize testTableView;
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
}
return self;
}
- (void)dealloc
{
[self.labels release];
[self.images release];
[self.testTableView release];
[super dealloc];
}
- (void)didReceiveMemoryWarning
{
// Releases the view if it doesn't have a superview.
[super didReceiveMemoryWarning];
// Release any cached data, images, etc that aren't in use.
}
#pragma mark - View lifecycle
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view from its nib.
self.title = @"Test";
self.labels = [[NSArray alloc] initWithObjects:
@"Test 1",
@"Test 2",
@"Test 3",
@"Test 4",
@"Test 5",
@"Test 6",
@"Test 7",
@"Test 8",
@"Test 9",
@"Test 10",
@"Test 11",
@"Test 12",
@"Test 13",
@"Test 14",
@"Test 15",
@"Test 16",
@"Test 17", nil];
self.images = [[NSArray alloc] initWithObjects:
[UIImage imageNamed:@"empty1.png"],
[UIImage imageNamed:@"empty2.png"],
[UIImage imageNamed:@"empty3.png"],
[UIImage imageNamed:@"empty4.png"],
[UIImage imageNamed:@"empty5.png"],
[UIImage imageNamed:@"empty6.png"],
[UIImage imageNamed:@"empty7.png"],
[UIImage imageNamed:@"empty8.png"],
[UIImage imageNamed:@"empty9.png"],
[UIImage imageNamed:@"empty10.png"],
[UIImage imageNamed:@"empty11.png"],
[UIImage imageNamed:@"empty12.png"],
[UIImage imageNamed:@"empty13.png"],
[UIImage imageNamed:@"empty14.png"],
[UIImage imageNamed:@"empty15.png"],
[UIImage imageNamed:@"empty16.png"],
[UIImage imageNamed:@"empty17.png"], nil];
self.testTableView.separatorColor = [UIColor colorWithRed:0 green:0.21 blue:0.43 alpha:1];
}
- (void)viewDidUnload
{
[super viewDidUnload];
// Release any retained subviews of the main view.
// e.g. self.myOutlet = nil;
self.labels = nil;
self.images = nil;
self.testTableView = nil;
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
// Return YES for supported orientations
return (interfaceOrientation == UIInterfaceOrientationPortrait);
}
#pragma mark - Table view data source methods
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
// Return the cell's height
return kTestCellHeight;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return [self.labels count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *TestCellIdentifier = @"TestCellIdentifier";
TestCell *cell = (TestCell *)[tableView dequeueReusableCellWithIdentifier:TestCellIdentifier];
if (cell == nil) {
NSArray *nib = [[NSBundle mainBundle] loadNibNamed:@"TestCell"
owner:self
options:nil];
for (id oneObject in nib) {
if ([oneObject isKindOfClass:[TestCell class]]) {
cell = (TestCell *)oneObject;
}
}
}
NSInteger row = [indexPath row];
cell.testLabel.text = [self.labels objectAtIndex:row];
cell.testImage.image = [self.images objectAtIndex:row];
return cell;
}
@end
必要なコンテキストの説明はこれですべてだと思います。今問題。テーブル内のスイッチの 1 つをオフ (デフォルトではオン) に設定し、下にスクロールしてからもう一度上にスクロールすると、テーブルはランダムなスイッチをオフとオンに微調整し始めます。これはすべて非常にランダムです。
なぜこれが起こるのか、またはそれを回避する方法を知っている人はいますか?