0

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

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

}
4

2 回答 2

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;


}
于 2013-02-25T13:19:43.117 回答
0

1)使用所有按钮创建一个自定义单元格

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

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

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

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

即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)在控制器中:

在: - (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){...
    }
于 2016-08-20T10:32:56.393 回答