从uitableviewcell调用并使用uitableview?

时间:2015-06-23 09:13:55

标签: ios objective-c iphone xcode tableviewcell

我有一个带有许多UITableViewCell的UITableViewController。每个Cell都有一个UISwitch按钮。

这是我的UITableViewController类:

@implementation DanhsachTableViewController{
    NSMutableArray *data;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    [self loadData];
}

    - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
        DichVu2TableViewCell *cell = (DichVu2TableViewCell *)[tableView dequeueReusableCellWithIdentifier:@"dscell"];
        NSDictionary *dataCell =  data[indexPath.row];
        cell.service =  dataCell[@"service"];
        cell.package =  dataCell[@"package"];
        cell.loai_goi = dataCell[@"loai_goi"];

        return cell;
    }
-(void) changeCellState:(NSString *)service package:(NSString *)package loaigoi:(NSString *)loai_goi{
    for (int i =0;i<data.count;i++){
        DichVu2TableViewCell *cellLocal = [self.tableView cellForRowAtIndexPath:[NSIndexPath indexPathForRow:i inSection:0]];
        if ([service isEqualToString:cellLocal.service] && ![package isEqualToString:cellLocal.package] && [loai_goi isEqualToString:cellLocal.loai_goi]){
            [cellLocal.sudung setOn:NO animated:YES];
        }
     }
}

“数据”数组在方法loadData中加载(这里不重要,所以我不包含它)。

在UITableViewCell(类名:DichVu2TableViewCell)中,我设置Switch的事件值更改,以便每次Switch更改值(例如ON)时,具有相同值“service”和“loai_goi”的所有其他单元格的开关将设置为OFF。

DanhsachTableViewController *tableview = [[DanhsachTableViewController alloc] init];
    tableview.tableView.delegate = (DanhsachTableViewController *)self.superview.superview;
[tableview changeCellState:_service package:_package loaigoi:_loai_goi];

但是当我调用时,上面tableview的数组“data”有0个对象,所以什么也没发生。

有没有办法做到这一点?

嗨Oyeoj, 谢谢你的帮助。

跟着你的导游我有点问题。当我使用提取代码时,xcode中有一些错误,所以我必须自定义。但程序在运行时仍然有错误。你能帮我看看我的代码吗?提前谢谢。

DichVu2TableViewCell.h

@class DichVu2TableViewCell;
//Change : NSObject to <NSObject> because XCode 6.3 has error.
@protocol DichVu2TableViewCellDelegate <NSObject>

-(void)changeCell:(DichVu2TableViewCell *)sender state:(NSString *)service package:(NSString *)package loaigoi:(NSString *)loai_goi;

@end

@interface DichVu2TableViewCell : UITableViewCell

@property (weak) id <DichVu2TableViewCellDelegate> delegate;

@end

DichVu2TableViewCell.m

@implementation DichVu2TableViewCell
….
- (void)someSwitchingEvent
{
    [self.delegate changeCell:self state:_service package:_package loaigoi:_loai_goi];
}
@end

DanhsachTableViewController.h

@interface DanhsachTableViewController : UITableViewController

@property (strong,nonatomic) NSString *loaitb;
@property (strong,nonatomic) NSString *type;
@property (strong,nonatomic) NSString *name;
@property NSMutableArray *pre_inuse_;
@property NSMutableArray *data_inuse_;
@property NSMutableArray *vas_inuse_;
@end

DanhsachTableViewController.m

#import "DichVu2TableViewCell.h"
//Change <DichVu2TableViewCellDelegate> to (DichVu2TableViewCellDelegate) because XCode 6.3 has error.
@interface DanhsachTableViewController (DichVu2TableViewCellDelegate)
@property (nonatomic) NSIndexPath *forUpdateIndexPath;
@end

@implementation DanhsachTableViewController{
    NSMutableArray *data;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    DichVu2TableViewCell *cell = (DichVu2TableViewCell *)[tableView dequeueReusableCellWithIdentifier:@"dscell"];
    NSDictionary *dataCell =  data[indexPath.row];
    cell.service =  dataCell[@"service"];
    cell.package =  dataCell[@"package"];
    cell.loai_goi = dataCell[@"loai_goi"];
    cell.kieu_goi = dataCell[@"kieu_goi"];

    [cell.sudung setOn:NO animated:YES];
    cell.delegate = self;
//Change cellLocal —> cell because there are no cellLocal avaiable. And Program error when run to this row.
    [cell.sudung setOn:(self.forUpdateIndexPath == indexPath) animated:YES];

    return cell;
}

-(void)changeCell:(DichVu2TableViewCell *)sender state:(NSString *)service package:(NSString *)package loaigoi:(NSString *)loai_goi
{
//Add cellLocal —> cell because there are no cellLocal avaiable
    DichVu2TableViewCell *cellLocal = (DichVu2TableViewCell *)[self.tableView cellForRowAtIndexPath:self.forUpdateIndexPath];
    if ([service isEqualToString:cellLocal.service] && ![package isEqualToString:cellLocal.package] && [loai_goi isEqualToString:cellLocal.loai_goi]){
        // get the indexPath of the cell
        self.forUpdateIndexPath =  [self.tableView indexPathForCell:sender];
        // update the date source
        NSMutableDictionary *dataCell =  [data[self.forUpdateIndexPath.row] mutableCopy];
        [dataCell setObject:service forKey:@"service"];
        [dataCell setObject:package forKey:@"package"];
        [dataCell setObject:loai_goi forKey:@"loai_goi"];
        // you dont need the for-statement just reload the table
        [self.tableView reloadData];
        // then update the switch inside `- cellForRowAtIndexPath`
    }

}

2 个答案:

答案 0 :(得分:0)

您是否尝试过记录self.superview.superview;?您确定属于UIViewController类型吗?

如果是这样的话:

请勿致电[[DanhsachTableViewController alloc]

而是使用您父视图中的DanhsachTableViewController

DanhsachTableViewController *tableview = (DanhsachTableViewController *)self.superview.superview;
[tableview changeCellState:_service package:_package loaigoi:_loai_goi];

这个

DanhsachTableViewController *tableview = [[DanhsachTableViewController alloc] init];分配 DanhsachTableViewController课程而不是现有课程。

修改

使用protocoldelegate

使用委托时,您不需要self.superview.superview:)

  

UNDER DichVu2TableViewCell

@class DichVu2TableViewCell; 

@protocol DichVu2TableViewCellDelegate <NSObject>

-(void)changeCell:(DichVu2TableViewCell *)sender state:(NSString *)service package:(NSString *)package loaigoi:(NSString *)loai_goi;

@end

@interface DichVu2TableViewCell : UIViewController

@property (weak) id <DichVu2TableViewCellDelegate> delegate;

@end

@implementaion DichVu2TableViewCell

..

- (void)someSwitchingEvent
{
    [self.delegate changeCell:self state:_service package:_package loaigoi:_loai_goi];
}

@end

,而

  

UNDER DanhsachTableViewController

// assuming you alreading imported the `DichVu2TableViewCell` like 
//
// #import "DichVu2TableViewCell.h"
//
// set the delegate

@interface DanhsachTableViewController () <DichVu2TableViewCellDelegate>

@property (nonatomic) NSIndexPath *forUpdateIndexPath;

@end

// then implement the method from the delegate under implementation file

@implementation DanhsachTableViewController
{
    NSMutableArray *data;
}

- (void)viewDidLoad 
{
    [super viewDidLoad];
    [self loadData];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    DichVu2TableViewCell *cell = (DichVu2TableViewCell *)[tableView dequeueReusableCellWithIdentifier:@"dscell"];
    NSDictionary *dataCell =  data[indexPath.row];
    cell.service =  dataCell[@"service"];
    cell.package =  dataCell[@"package"];
    cell.loai_goi = dataCell[@"loai_goi"];

    // this is the magic .. :)
    //--
    cell.delegate = self;

    [cell.sudung setOn:(self.forUpdateIndexPath == indexPath) animated:YES];
    //--

    return cell;
}

-(void)changeCell:(DichVu2TableViewCell *)sender state:(NSString *)service package:(NSString *)package loaigoi:(NSString *)loai_goi
{
    // `self.forUpdateIndexPath` is declared as property

   if ([service isEqualToString:service] && ![package isEqualToString:package] && [loai_goi isEqualToString:loai_goi]){

        // get the indexPath of the cell
        self.forUpdateIndexPath =  [self.tableView indexPathForCell:sender];

        // update the date source
        NSMutableDictionary *dataCell =  [data[self.forUpdateIndexPath.row] mutableCopy];

        [dataCell setObject:service forKey:@"service"];
        [dataCell setObject:package forKey:@"package"];
        [dataCell setObject:loai_goi forKey:@"loai_goi"];

        // you dont need the for-statement just reload the table

        [self.tableView reloadData];

        // then the switch will be updated inside `- cellForRowAtIndexPath`
    }

}

希望现在更有帮助.. :)干杯......

答案 1 :(得分:0)

更有效的方法是使用自定义委托。

  1. 您可以在UITableViewController课程中声明协议。
  2. 在协议中声明changeCellState函数。
  3. UITableViewCell类中创建委托属性。
  4. 当切换值更改时,从UITableViewCell类调用委托方法。
  5. UITableViewController本身将收到消息,并且将分别调用该函数。