【问题标题】:How to disable a buttons in a custom cell?如何禁用自定义单元格中的按钮?
【发布时间】:2013-09-02 06:33:26
【问题描述】:

我有一个自定义单元格,它使用包含以下组件的情节提要:

1 UIImage 2个UILabel 4 个 UI 按钮

一个按钮允许用户将优惠券保存为收藏夹,我想在用户保存优惠券后将此按钮设置为禁用,我尝试为自定义单元格类中的按钮添加一个 IBOutlet 但它不起作用,我不要得到任何错误。我怎样才能做到这一点?有人可以帮助我吗?

守则:

CouponsCell.h

@property (nonatomic, strong) IBOutlet UIButton *saveFav;

CouponsCell.m

#import "CouponsCell.h"

@implementation CouponsCell
@synthesize saveFav;

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

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

    // Configure the view for the selected state
}
@end

我已经在 IBOutlet 和按钮之间建立了连接,当用户触摸按钮时,我尝试了这个:

- (IBAction)AddFavorite:(id)sender {

    CGPoint buttonPosition = [sender convertPoint:CGPointZero toView:self.tableView];
    NSIndexPath * indexPath = [self.tableView indexPathForRowAtPoint:buttonPosition];
    Coupons * couponsObjects = [self.fetchedResultsController objectAtIndexPath:indexPath];

    CouponsCell *couponsCell = [self.tableView dequeueReusableCellWithIdentifier:@"CouponCell" forIndexPath:indexPath];

    idCoupon = cuponesObjects.idCoupons;

    Reachability *reachability = [Reachability reachabilityForInternetConnection];
    [reachability startNotifier];
    NetworkStatus status = [reachability currentReachabilityStatus];

    if ( status == NotReachable )
    {
        UIAlertView *message = [[UIAlertView alloc] initWithTitle:@"CC Galerias"
                                                          message:@"Can’t save the Coupon to favorite, Check your Internet connection."
                                                         delegate:nil
                                                cancelButtonTitle:@"OK"
                                                otherButtonTitles:nil];

        [message performSelectorOnMainThread:@selector(show) withObject:nil waitUntilDone:YES];
    }
    else if ( status == ReachableViaWiFi )
    {
        [self postCouponFav:idCoupon]; // Save the Coupon to Favorites
        [couponsCell.saveFav setEnabled:NO]; // Set diseable the button


    }
    else if ( status == ReachableViaWWAN )
    {
        [self postCouponFav:idCoupon];
        [couponsCell.saveFav setEnabled:NO];

    }

}

请帮助我,也许解决方案很简单,但我正在学习 iOS 开发。提前感谢您的帮助。

【问题讨论】:

  • 请更改您的主题以使用混合大小写(不要大喊大叫)并将其更改为反映您的问题的正确标题。
  • 如果您在 AddFavorite 方法中放置一个断点,执行是否会停止?
  • 对不起,我是stackoverflow的新手,我已经改了标题...
  • @tdelepine 是的,它是...优惠券已保存为收藏,但不会将状态更改为禁用。

标签: ios objective-c


【解决方案1】:

您不能使用出队,因为返回单元格是单元格的新实例,而不是显示的单元格。

你有两种改变按钮状态的方法。

  • 在您的自定义单元类中而不是在控制器类中传输您的 (IBACTION) 方法并发布旧代码。如果您需要在 CustomCell 和控制器之间更新数据,请创建并调用委托。

  • 第二种方法是在为呈现井状态设置必要的值后重新加载 UITableView 的 Cell

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2011-02-14
    • 1970-01-01
    • 2023-02-07
    • 2010-11-05
    • 2016-05-11
    • 2010-09-10
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多