单击按钮在tableViewCell中配置数据

时间:2013-02-25 12:16:23

标签: ios objective-c uitableview uilabel

我正在使用我的coreData属性配置UITableViewCell。我能够显示文本。但是我想在按钮点击时用其他数据重新加载单元格。

-(void)configureCell:(UITableViewCell *)cell atIndexPath:(NSIndexPath *)indexPath
{
  Book *entity = [self.fetchedResultsController objectAtIndexPath:indexPath];
  firstLabel = [[UILabel alloc]initWithFrame:CGRectMake(0, 10, 120, 21)];
  secondLabel = [[UILabel alloc]initWithFrame:CGRectMake(120, 10, 120, 21)];

  firstLabel.text = entity.title;
  secondLabel.text = entity.author;

  [cell.contentView addSubview:firstLabel];
  [cell.contentView addSubview:secondLabel];

  //On button Click i want to change firstLabel.text = entity.date and secondlabel.text =   entity.details

}

2 个答案:

答案 0 :(得分:1)

试试这段代码..

设置全局变量

int buttonCliked;

设置按钮

的操作
- (IBAction)aaaa:(id)sender
{
    buttonCliked =1;
    [_myTable reloadData];
}

使用数据加载tableviewCell

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"Cell";

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
    cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];

Book *entity = [self.fetchedResultsController objectAtIndexPath:indexPath];

firstLabel = [[UILabel alloc]initWithFrame:CGRectMake(0, 10, 120, 21)];
secondLabel = [[UILabel alloc]initWithFrame:CGRectMake(120, 10, 120, 21)];

if(!buttonClicked)
{
    firstLabel.text = entity.title;
    secondLabel.text = entity.author;
}
else
{
    firstLabel.text =  entity.date;
    secondLabel.text = entity.details;

    if(indexPath.row >= [tableView numberOfRowsInSection:indexPath.section] && indexPath.section >= [tableView numberOfSections])
    {
        buttonCliked =0;
    }

}

[cell.contentView addSubview:firstLabel];
[cell.contentView addSubview:secondLabel];

return cell;


}

答案 1 :(得分:0)

1)创建一个包含所有按钮的自定义单元格

2)为你喜欢的每个事件创建一个枚举(即SendType,LikeType等)。

3)为您的单元格创建协议,并且委托必须是控制器 对于每个细胞。

4)您的六个按钮的操作必须保留在单元格中,并且单元格将调用其委托为您的事件传递ENUM,PLUS单元格参考。

5)控制器将由所需的协议方法调用,并决定使用单元格ref和事件类型做什么。

即。 1)创建一个自定义单元格(ContactCell)并添加:

property (id<TapOnSixButtonsProtocol>) tapDelegate;

  - (void)fillCellWithID:(NSString*)ID
             idContact: (NSString*)idContact
          withDelegate: (id<TapOnSixButtonsProtocol>) tapDelegate;
{
    self.tapDelegate = tapDelegate

2)

typedef NS_ENUM(NSInteger, CustomTypeOfEvent) {
    Like = 1,
    Send =  2,..
};

3)

   @protocol TapOnSixButtonsProtocol <NSObject>
    -(void)didClickonCell: (CustomCell*)cell withType:(CustomTypeOfEvent)t;
    @end

4)

self.Btn1 = [UIButton buttonWithType:UIButtonTypeCustom];
[self.Btn1  addTarget:self
                  action:@selector(tapped:)
        forControlEvents:UIControlEventTouchUpInside];
self.Btn1.tag = Like;
(repeat for all...)

...

   void tapped:(UIBUtton*)sender{
        CustomTypeOfEvent t = sender.tag // get back type..
        [self.tapDelegate didClickOnCell: self withType];

5)在控制器中:

in: - (UITableViewCell *)tableView:(UITableView *)tableView

cellForRowAtIndexPath:(NSIndexPath *)indexPath {
  ContactCell cell = [tableView dequeueReusable..
   [cell fillCellWithID: @"234444"
                 idContact: "67899999"
              withDelegate: self];


//delegate:
    -(void)didClickOnCell: (CustomCell*)cell withType:(CustomTypeOfEvent)t {
       switch(t){...
    }