ios - UITableView 的最后一个单元格在一段时间后自动消失?

标签 ios objective-c uitableview

我在一个 View Controller 中有一个 TableView ,其中有五个单元格,单击这些单元格我将重定向到其他 View Controller 。问题是每当我返回包含 TableView 的 View Controller 时, TableView 中最后一个单元格的内容会自动消失。

这是我的表格 View 的代码:

-(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;
}

logonamelogoimage 是两个 NSMutableArrays

如果需要更多代码,请告诉我,我会提供必要的代码。

最佳答案

最好你需要对 tableview 进行子类化,因为每次你都在重用单元格,但它的 subview 不是,所以就这样做

创建一个新文件并将其命名为 UITableViewCellMyCustomCell 子类 在 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

在 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;
 }

希望这对你有帮助:)

关于ios - UITableView 的最后一个单元格在一段时间后自动消失?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23538850/

相关文章:

iphone - iOS:Arcgis:featureLayer.types 返回 Null

objective-c - 如何在 Swift 中将自定义 Objc 结构转换为 NSValue?

objective-c - EXC_BAD_ACCESS 在初始化时在自定义类上设置属性时?

ios - 如何将 URL 作为图像加载?

iOS TableView : How to fix 1st row but allow re-ordering of all other rows?

android - 如何在 React Native 中将按钮链接到详细信息屏幕

ios - 无法通过中继服务器连接到 SMP 2.3 - iOS 应用程序

android - 在智能设备上激活的 mqtt 客户端可用

ios - PSPDFKit SDK 崩溃

ios - 在编辑模式下移除表格行上的删除按钮 ⛔️