• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    迪恩网络公众号

ios - 无法使用自定义单元格更改某些属性

[复制链接]
菜鸟教程小白 发表于 2022-12-12 11:48:44 | 显示全部楼层 |阅读模式 打印 上一主题 下一主题

我正在为我的应用定制一个 UITableViewCell,因为我已经做了几次,所以一切都很好。

现在我想做一些动画,问题来了。

我会尽可能简短地解释它:

我正在做一个字典应用程序。我有一个 View ,其中包含一个 UITableView 和我的自定义单元格 (NTNHistoryCell),此 View 用于显示用户研究的历史记录。

#import <UIKit/UIKit.h>
#import "HistoryItem.h"

@protocol NTNHistoryCellDelegate;
@interface NTNHistoryCell : UITableViewCell<UIGestureRecognizerDelegate>

@property (strong, nonatomic) HistoryItem *historyItem;

@property (strong, nonatomic) IBOutlet UILabel *lblWord;
@property (strong, nonatomic) IBOutlet UILabel *lblDictName;
@property (strong, nonatomic) IBOutlet UILabel *lblTimeStamp;

//variables for editing
@property (strong, nonatomic) IBOutlet UIImageView *imgDeleteBackground;
@property (strong, nonatomic) IBOutlet UIView *frontView;
@property (strong, nonatomic) UIImageView *imgIconDelete;

@property (strong, nonatomic) id<NTNHistoryCellDelegate> delegate;

-(void)initLabels;
@end

@protocol NTNHistoryCellDelegate <NSObject>

@optional

-(void)historyCellIsDeletedNTNHistoryCell*)historyCell;

@end

用户可以向左或向右滑动以删除条目。 单元格上有两个重要的东西:作为 UIImageView 的背景,用于在用户滑动时更改颜色,作为 UIView 的 frontView 包含一些 UILabel,frontView 将在用户滑动时更改其框架。如果用户滑动翻译,则单元格将被删除(对应的条目在数据库中也被删除)。

现在我想做动画让用户知道他们可以在每次加载 tableview 时滑动单元格。我将事件 didEndDisplayingCell 与以下代码一起使用:

-(void)tableViewUITableView *)tableView didEndDisplayingCellUITableViewCell *)cell forRowAtIndexPathNSIndexPath *)indexPath
{
    //do cell animation to inform user that the cell can be swiped to delete
    //only for the 1st row    
    if(indexPath.row == ((NSIndexPath*)[[tableView indexPathsForVisibleRows] lastObject]).row)
    {        
        NTNHistoryCell *cell = (NTNHistoryCell *)[tableView cellForRowAtIndexPath:[NSIndexPath indexPathForRow:0 inSection:0]];
        CGRect originalFrame = cell.frontView.frame;
        [UIView beginAnimations"move" context:nil];
        [UIView setAnimationDuration:1.0];

        //move left

        //not working!
        cell.frontView.frame = CGRectMake(-originalFrame.size.width, originalFrame.origin.y, originalFrame.size.width, originalFrame.size.height);
        cell.lblWord.text = @"text change test";//not working!
        cell.lblDictName.text = @"text change test";//not working!
        cell.frontView.backgroundColor = [UIColor greenColor];//work fine!

        [UIView commitAnimations];
    }
}

所有代码都不顺利,你可以看我的评论,我可以改变背景颜色,但UILabel的文本和frontView的框架。

我打算向左移动,然后向右移动 frontView。

我哪里做错了?

为了澄清我的话,这是截图(我没有足够的声誉来发布图片):http://i.stack.imgur.com/dUddJ.png

编辑: 我解决了。 在@danypata 回复中查看我的评论。



Best Answer-推荐答案


因此,您需要检测表格 View 何时完成加载过程,然后为第一个可见单元格制作动画。所以这是我的方法:

-(void)viewDidLoad {
  [super viewDidLoad];
  shouldAnimate = YES;
}

//delegate method
-(void) tableViewUITableView *)tableView willDisplayCellUITableViewCell *)cell forRowAtIndexPathNSIndexPath *)indexPath
{  if(shouldAnimate) {
       if([indexPath row] == ((NSIndexPath*)[[tableView indexPathsForVisibleRows] lastObject]).row){
       //now the table view completes the loading process
       [self perforAnimation];
     }
}

-(void)performAnimation {
   shouldAnimate = NO;
   NSIndexPath *firstCelPath = [[yourTableView indexPathsForVisibleCells] objectAtIndex:0]
   YourCustomCell *cell = [yourTableView cellForRowAtIndexPath:firstCellPath];
   [UIView animateWithDuration:0.3 animations:^{
     //do your changes
   }];
}

我不能 100% 确定这种方法是否可行,但您应该尝试一下。

关于ios - 无法使用自定义单元格更改某些属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17074954/

回复

使用道具 举报

懒得打字嘛,点击右侧快捷回复 【右侧内容,后台自定义】
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

关注0

粉丝2

帖子830918

发布主题
阅读排行 更多
广告位

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap