質問

I have a table view in one view controller with five cells on clicking of which I redirect to some other view controller. The issue is that whenever I come back the view controller that contains the table view, the contents of the last cell in the table view automatically disappears.

Here is the code for my tableview:

-(void)viewWillAppear:(BOOL)animated
{
    logoimg=[[NSMutableArray alloc]initWithObjects:@"account_settings.png", @"account_settings_onclick.png",@"invite_friends.png", @"invite_friends_onclick.png", @"leaderboard.png", @"leaderboard_onclick.png", @"setting.png",@"setting_onclick.png", @"logout.png", @"logout_onclick.png", nil];
    logoname=[[NSMutableArray alloc]initWithObjects:@"Account Settings", @"Invite Friends",@"Leaderboard",@"Settings",@"Logout",nil];
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 45;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section       {
    return logoname.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    NSString *CellIdentifier = [NSString stringWithFormat:@"%d_%d",indexPath.section,indexPath.row];
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
    }
    cell.backgroundColor = [UIColor clearColor];
    //[[[cell contentView] subviews] makeObjectsPerformSelector: @selector(removeFromSuperview)];
    UIImage *logoOff = [UIImage imageNamed:[logoimg objectAtIndex:indexPath.row*2]];
    UIImage *logoOn = [UIImage imageNamed:[logoimg objectAtIndex:indexPath.row*2+1]];
    UIButton *logo = [UIButton buttonWithType:UIButtonTypeRoundedRect];
    [logo setBackgroundImage:logoOff forState:UIControlStateNormal];
    [logo setBackgroundImage:logoOn forState:UIControlStateSelected];
    [logo setBackgroundImage:logoOn forState:UIControlStateHighlighted];
    logo.frame = CGRectMake(20, 5, logoOff.size.width/2, logoOff.size.height/2);
    logo.userInteractionEnabled = NO;
    logo.tag = indexPath.row;
    logo.selected = FALSE;
    //[bottomView addSubview:logo];


    UILabel *name = [[UILabel alloc]initWithFrame:CGRectMake(70,13, cell.contentView.frame.size.width-70, 20)];
    [name setFont:[UIFont fontWithName:@"CenturyGothic" size:14]];
    name.numberOfLines = 2;
    name.text=[logoname objectAtIndex:indexPath.row];
    name.backgroundColor = [UIColor clearColor];
    [name sizeToFit];
    name.textColor = [UIColor whiteColor];

    UIImageView *divider = [[UIImageView alloc]init];
    divider.image = [UIImage imageNamed:@"divider_line.png"];
    divider.frame = CGRectMake(0, 43, self.view.frame.size.width ,2);

    [cell.contentView addSubview:logo];
    [cell.contentView addSubview:name];
    [cell.contentView addSubview:divider];

    return cell;
}

The logoname and logoimage are two NSMutableArrays.

If any more code is needed let me know I will provide the necessary code.

役に立ちましたか?

解決

Better u need to subclass the tableview because, each time you are reusing the cell, but its subview are not, so just do like this

create a new file and name it as MyCustomCell subclass of UITableViewCell in MyCustomCell.h file

 MyCustomCell.h
 #import <UIKit/UIKit.h>

 @interface MyCustomCell : UITableViewCell
 @property (nonatomic, retain) UIImage *logoOff;
 @property (nonatomic, retain) UIImage *logoOn;
 @property (nonatomic, retain) UIButton *logo;
 @property (nonatomic, retain) UILabel *name;
 @property (nonatomic, retain) UIImageView *divider;

 @end

in MyCustomCell.m file

 MyCustomCell.m
 #import "MyCustomCell.h"

 @implementation MyCustomCell
 @synthesize logoOn;
 @synthesize logoOff;
 @synthesize logo;
 @synthesize name;
 @synthesize divider;


 - (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
   self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
   if (self) {
    // Initialization code
    self.logo = [UIButton buttonWithType:UIButtonTypeRoundedRect];
    self.logo.userInteractionEnabled = NO;
    self.logo.selected = FALSE;

    self.name = [[UILabel alloc]initWithFrame:CGRectZero];
    [self.name setFont:[UIFont fontWithName:@"CenturyGothic" size:14]];
    self.name.numberOfLines = 2;

    self.name.backgroundColor = [UIColor clearColor];
    [self.name sizeToFit];
    self.name.textColor = [UIColor whiteColor];

    self.divider = [[UIImageView alloc]init];

    [self.contentView addSubview:logo];
    [self.contentView addSubview:name];
    [self.contentView addSubview:divider];

  }
   return self;
}

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

    // Configure the view for the selected state
 }

 - (void)layoutSubviews
{
   [super layoutSubviews];
   //  set the frames for all subviews
   [self.logo setBackgroundImage:self.logoOff forState:UIControlStateNormal];
   [self.logo setBackgroundImage:self.logoOn forState:UIControlStateSelected];
   [self.logo setBackgroundImage:self.logoOn forState:UIControlStateHighlighted];

    self.logo.frame = CGRectMake(20, 5, self.logoOff.size.width/2, self.logoOff.size.height/2);
    self.name.frame = CGRectMake(70,13, self.bounds.size.width-70, 20);
    self.divider.frame = CGRectMake(0, 43, self.bounds.size.width ,2);

}


@end

and in the controller

  - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
 {

     MyCustomCell *cell = [tableView dequeueReusableCellWithIdentifier:@"CELL"];
     if(cell == nil)
     {
        cell = [[MyCustomCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"CELL"];
     }
     cell.logoOff = [UIImage imageNamed:[logoimg objectAtIndex:indexPath.row*2]];
     cell.logoOn  = [UIImage imageNamed:[logoimg objectAtIndex:indexPath.row*2]];
     cell.name.text =  [logoname objectAtIndex:indexPath.row];
    // cell.divider.image = [UIImage imageNamed:@"divider_line.png"]; //for test i commented 
    cell.divider.backgroundColor = [UIColor blackColor];

    return cell;
 }

Hope this helps u :)

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top