tablecell背景仅显示自定义单元格,但显示自定义单元格使其无法选择

时间:2015-06-21 22:40:02

标签: ios objective-c uitableview background didselectrowatindexpath

我创建了一个自定义tableCell当我通过backgroundView将xib连接到文件所有者时,我成功地将项目显示在我的表中。问题是我无法选择一行并获得回报。

table cells work

如果我取消选择backgroundView,那么我的自定义单元格不会填充,但我可以选择一个单元格并提取通知。我显然希望这两个项目都有效。显示我的自定义单元格,并能够选择特定单元格。 clicking works

GameTableCell.m

#import "GameTableCell.h"

@implementation GameTableCell
@synthesize GameTime, AwayImage, HomeImage;


- (void)awakeFromNib {
    // Initialization code
}


- (void)setSelected:(BOOL)selected animated:(BOOL)animated
{
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}
/*
- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        // Initialization code
    }
    return self;
}
*/

-(id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    NSArray *topLevelObjects = [[NSBundle mainBundle] loadNibNamed:@"GameTableCell" owner:self options:nil];
    self = [topLevelObjects objectAtIndex:0];
    return self;
}

@end

GameTable.h:

#import <UIKit/UIKit.h>

@interface GameTableCell : UITableViewCell
@property (weak, nonatomic) IBOutlet UILabel *GameTime;
@property (weak, nonatomic) IBOutlet UIImageView *AwayImage;
@property (weak, nonatomic) IBOutlet UIImageView *HomeImage;


@end

HomePage.m:

 #import "HomePage.h"
    #import "PickSport.h"
    #import "GameTableCell.h"

    static NSString *CellIdentifier = @"GameTableCell";
    NSString *headerText;

    @interface HomePage ()

    @end

    @implementation HomePage
    @synthesize ActiveChal,PendingChal;

    -(IBAction)makeChallenge:(id)sender{
        PickSport *second = [[PickSport alloc] initWithNibName:@"PickSport" bundle:nil];
        [self presentViewController:second animated:YES completion:nil];
    }

    - (void)viewDidLoad {
        [super viewDidLoad];

        // Arrays for filling table cells
        _HomeImages=@[@"ic_bengals_nfl",@"ic_bengals_nfl",@"ic_bengals_nfl",@"ic_bengals_nfl", ];
        _AwayImages=@[@"ic_bears_nfl",@"ic_bears_nfl",@"ic_bears_nfl",@"ic_bears_nfl", ];
        _SportGameInfo=@[@"7:00pm",@"8:00pm",@"9:00pm",@"10:00pm",];
        [self.PendingChal registerClass:[GameTableCell class] forCellReuseIdentifier:CellIdentifier];
        [self.ActiveChal registerClass:[GameTableCell class] forCellReuseIdentifier:CellIdentifier];


    }

    - (void)didReceiveMemoryWarning {
        [super didReceiveMemoryWarning];
        // Dispose of any resources that can be recreated.
    }


    - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
    {
        return (interfaceOrientation != UIInterfaceOrientationPortraitUpsideDown);
    }
    //Set up the table props
    - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
        return 1;
    }

    //number of rows in the table
    - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {

        // rows eqiv to length of array SportGameinfo
        return _SportGameInfo.count;
    }


    - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
        static NSString *CellIdentifier=@"GameTableCell";
          GameTableCell *cell = (GameTableCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];

        //GameTableCell *cell = [tableView dequeueReusableCellWithIdentifier: CellIdentifier];

        if (cell == nil)
        {
            NSArray *nib = [[NSBundle mainBundle] loadNibNamed:CellIdentifier owner:self options:nil];
            cell = [nib objectAtIndex:0];
        }

       if(tableView == PendingChal){
               NSLog(@"in Pend table");
       int row =[indexPath row];
           cell.HomeImage.image=[UIImage imageNamed:[_HomeImages objectAtIndex:indexPath.row]]; //thumbnails objectAtIndex:indexPath.row]
            cell.AwayImage.image=[UIImage imageNamed:[_AwayImages objectAtIndex:indexPath.row]];
            cell.GameTime.text=[_SportGameInfo objectAtIndex:indexPath.row];
                                     }

       if(tableView == ActiveChal){
            NSLog(@"in Active table");
            int row =[indexPath row];
            cell.GameTime.text=_SportGameInfo[row];
            cell.HomeImage.image=[UIImage imageNamed:_HomeImages[row]];
            cell.AwayImage.image=[UIImage imageNamed:_AwayImages[row]];
            }
        return cell;
    }


    -(UIView*)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
    {
        // 1. The view for the header
        UIView* headerView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, tableView.frame.size.width, 22)];
        if(tableView == PendingChal){
            headerText= @"Pending Challenges (waiting on you)";
        }
        else {
            headerText= @"Active Challenges";
                }
        // 2. Set a custom background color and a border
        headerView.backgroundColor = [UIColor colorWithWhite:0.5f alpha:1.0f];
        headerView.layer.borderColor = [UIColor colorWithWhite:0.5 alpha:1.0f].CGColor;
        headerView.layer.borderWidth = 0;

        // 3. Add a label
        UILabel* headerLabel = [[UILabel alloc] init];
        headerLabel.frame = CGRectMake(0, 0, tableView.frame.size.width -0, 20);
        headerLabel.backgroundColor = [UIColor blackColor];
        headerLabel.textColor = [UIColor whiteColor];
        headerLabel.font = [UIFont boldSystemFontOfSize:16.0];
        headerLabel.text = headerText;
        headerLabel.textAlignment = NSTextAlignmentLeft;
        // 4. Add the label to the header view
        [headerView addSubview:headerLabel];
        // 5. Finally return
        return headerView;
    }


    - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
    {
        NSLog(@"didSelectRowAtIndexPath");
        /*UIAlertView *messageAlert = [[UIAlertView alloc]
         initWithTitle:@"Row Selected" message:@"You've selected a row" delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil];*/

        UIAlertView *messageAlert = [[UIAlertView alloc]
                                     initWithTitle:@"Row Selected" message:[_SportGameInfo objectAtIndex:indexPath.row] delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil];

        // Display the Hello World Message
        [messageAlert show];

    }

    - (NSIndexPath *)tableView:(UITableView *)tableView willSelectRowAtIndexPath:(NSIndexPath *)indexPath
    {
        NSLog(@"willSelectRowAtIndexPath");
        return indexPath;
    }


    @end

HomePage.h:

#import <UIKit/UIKit.h>

@interface HomePage : UIViewController <UITableViewDataSource, UITableViewDelegate>

@property (strong, nonatomic) IBOutlet UITableView *PendingChal;
@property (strong, nonatomic) IBOutlet UITableView *ActiveChal;
@property (nonatomic, strong)NSArray *HomeImages;
@property (nonatomic, strong)NSArray *AwayImages;
@property (nonatomic, strong)NSArray *SportGameInfo;

@end

在这里完成我的HomePage.xib

的连接

connected xib

1 个答案:

答案 0 :(得分:1)

抛开你应用程序的奇怪行为,我注意到你没有以通常的方式呈现表格单元格。所以首先尝试以通常的方式呈现它:

  1. viewDidLoad中注册nib,因为您在xib中创建了单元格

    - (void)viewDidLoad {
        [super viewDidLoad];
    
        // Arrays for filling table cells
        _HomeImages=@[@"ic_bengals_nfl",@"ic_bengals_nfl",@"ic_bengals_nfl",@"ic_bengals_nfl", ];
        _AwayImages=@[@"ic_bears_nfl",@"ic_bears_nfl",@"ic_bears_nfl",@"ic_bears_nfl", ];
        _SportGameInfo=@[@"7:00pm",@"8:00pm",@"9:00pm",@"10:00pm",];
        [self.PendingChal registerNib:[GameTableCell nib] forCellReuseIdentifier:CellIdentifier];
        [self.ActiveChal registerNib:[GameTableCell nib] forCellReuseIdentifier:CellIdentifier];
    }
    
  2. - tableView:cellForRowAtIndexPath:中将可重复使用的单元格取消并将其返回

  3. 你不需要在tableViewCell的init方法中加载nib删除

    NSArray * topLevelObjects = [[NSBundle mainBundle] loadNibNamed:@&#34; GameTableCell&#34;所有者:自我选择:无; self = [topLevelObjects objectAtIndex:0];