我在一个视图控制器中有一个表视图,其中有五个单元格,我将其重定向到其他视图控制器。问题是每当我回到包含表视图的视图控制器时,表视图中最后一个单元格的内容会自动消失。
以下是我的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;
}
logoname 和 logoimage 是两个NSMutableArrays
。
如果需要更多代码,请告诉我我将提供必要的代码。
答案 0 :(得分:1)
更好的是你需要子类化tableview,因为每次你重用单元格,但它的子视图不是,所以就这样做
创建一个新文件并将其命名为MyCustomCell
UITableViewCell
的子类
在MyCustomCell.h
文件中
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
MyCustomCell.m文件中的
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
并在控制器中
- (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;
}
希望这有助于你:)