UItableViewCell中创建个UIButton在点击响应时出现异常

鑫鑫 发布于 2012/09/19 21:56
阅读 2K+
收藏 0
iOS
本人刚学objective-C不久,试着编了个UITableView的程序,在创建UITableViewCell中自定义了个UIButton,当写到
 [openbutton addTarget:self action:@selector(openButtonClicked) forControlEvents:UIControlEventTouchUpInside];
添加事件响应,仿真时点击按键,就出现了异常,导致系统退出,实在找不出问题所在,屏蔽上面的语句,按键可以正常点击。

异常为:




下面是本人编写的代码:
#import "LightCellViewController.h"

@implementation LightCellViewController

@synthesize tableViewOf;
@synthesize dataList;

- (void)loadView
{
    
    //加载plist文件的数据
    NSBundle *bundle = [NSBundle mainBundle];
    NSURL *plistURL = [bundle URLForResource:@"CustomList" withExtension:@"plist"];
    
    NSDictionary *dictionary = [NSDictionary dictionaryWithContentsOfURL:plistURL];
    
    NSMutableArray *tmpDataArray = [[NSMutableArray alloc] init];
    
    
    for (int i=0; i<[dictionary count]; i++) {
        
        
        NSString *key = [[NSString alloc] initWithFormat:@"%i", i+1];
        NSDictionary *tmpDic = [dictionary objectForKey:key];
        [tmpDataArray addObject:tmpDic];
        
        
        
    }
    self.dataList = [tmpDataArray copy];
    
    
    //定义TableView
    UIView *contentView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, 315)];
    contentView.autoresizesSubviews = YES;
    contentView.autoresizingMask = (UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight);
    contentView.backgroundColor = [UIColor clearColor];
    
    [self setView:contentView];
    
    
    tableViewOf = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, 320, 315) style:UITableViewStylePlain];
    [tableViewOf setAutoresizesSubviews:YES];
    [tableViewOf setAutoresizingMask:(UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight)];
    [tableViewOf setDataSource:self];
    [tableViewOf setDelegate:self];
    
    [tableViewOf setBackgroundColor:[UIColor colorWithRed:0.882 green:0.968 blue:1.0 alpha:1.0]];
    
    [[self view] addSubview:tableViewOf];
    
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{

    return [self.dataList count];
   
    
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 63;
}

- (UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    
    
    NSString *tag = @"tag";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:tag];
    
    if(cell == nil)
    {
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:tag] autorelease];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        cell.contentView.backgroundColor = [UIColor colorWithRed:0.882 green:0.968 blue:1.0 alpha:1.0];
        
        
        
        CGRect nameLabelRect = CGRectMake(10, 3, 60, 13);
        UILabel *nameLabel = [[UILabel alloc] initWithFrame:nameLabelRect];
        NSUInteger row = [indexPath row];
        NSDictionary *rowData = [self.dataList objectAtIndex:row];
        nameLabel.textAlignment = UITextAlignmentLeft;
        nameLabel.text = [rowData objectForKey:@"name"];
        nameLabel.textAlignment = UITextAlignmentCenter;
        nameLabel.font = [UIFont boldSystemFontOfSize:12];
        nameLabel.backgroundColor = [UIColor colorWithRed:0.882 green:0.968 blue:1.0 alpha:1.0];
        
        [cell.contentView addSubview:nameLabel];
        [nameLabel release];
        
        
        
        CGRect imageRect = CGRectMake(24, 20, 32, 32);
        UIImageView *image = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"灯光 不显示.png"]];
        image.frame = imageRect;
        [cell.contentView addSubview:image];
        [image release];
        
        
        
        
        CGRect imageAllButton = CGRectMake(80, 3, 220, 55);
        UIImageView *imageButtonBackground = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"中间 显示条.png"]];
        imageButtonBackground.frame = imageAllButton;
        [cell.contentView addSubview:imageButtonBackground];
        
        
        
        
        
        UIButton *openButton = [UIButton buttonWithType:UIButtonTypeCustom];
        CGRect ButtonFrame1 = CGRectMake(85, 10.5, 47.5, 37.5);
        [openButton setFrame:ButtonFrame1];
        [openButton setTitle:@"开" forState:UIControlStateNormal];
        [openButton setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
        [openButton setBackgroundImage:[UIImage imageNamed:@"中间 显示条-P.png"] forState:UIControlStateHighlighted];
        [openButton setShowsTouchWhenHighlighted:YES];
        [openButton.titleLabel setFont:[UIFont boldSystemFontOfSize:18]];
        openButton.backgroundColor = [UIColor clearColor];
        [openbutton addTarget:self action:@selector(openButtonClicked) forControlEvents:UIControlEventTouchUpInside];
        [cell.contentView addSubview:openButton];  
     
    }
    return cell;
}
- (void)openButtonClicked:(id)sender
{


}




- (void)dealloc
{
    [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

/*
// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad
{
    [super viewDidLoad];
}
*/

- (void)viewDidUnload
{
    [super viewDidUnload];
    // Release any retained subviews of the main view.
    // e.g. self.myOutlet = nil;
    self.dataList = nil;
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    // Return YES for supported orientations
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}

加载中
0
KSHime
KSHime

@selector(openButtonClicked:)

- (void)openButtonClicked:(id)sender

冒號不可少

0
鑫鑫
鑫鑫
谢谢 确实是这个问题
返回顶部
顶部