自定义UITableView的意外行为

时间:2011-03-31 22:01:54

标签: iphone uitableview uiswitch

我正在创建一个实现自定义UITableView的应用程序。我这样做的方式如下:

我创建了一个UINavigationController。这个UINavigationController推送一个实现UITableViewDelegate和UITableViewDataSource的UIViewController。我为这个UIViewController创建了一个.xib,因此为UIViewController提供了3个文件:TestTableViewController.h,TestTableViewController.m和TestTableViewController.xib。

TestTableViewController.xib只有一个UITableView放置链接到一个名为testTableView的IBOutlet UITableView,这个UITableView数据源和委托链接到File的所有者(TestTableViewController)。

另外,我已经实现了自定义UITableViewCell。我创建了一个名为TestCell.xib的空.xib,我放置了一个UITableViewCell并插入了UIView,UILabel,UIImageView和UISwitch。然后我创建了一个继承自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

我想这是所需的上下文的所有描述。现在问题。当我将表中的一个开关设置为OFF(它们默认为ON)然后向下滚动然后再向上时,表开始将随机开关调整为OFF和ON。这一切都是随机的。

任何人都知道为什么会这样或者如何避免它?

1 个答案:

答案 0 :(得分:0)

麦克沃思是对的。我只需要创建一个数组,其中包含每个UISwitch状态的布尔值,并将其分配给cellForrowAtIndexPath。