目标:当用户选择单元格时,将向该单元格添加一个按钮.在我的didSelectRowAtIndexPath函数中,我有以下内容:
UIButton *downloadButton = [[UIButton alloc] init]; downloadButton.titleLabel.text = @"Download"; [downloadButton setFrame:CGRectMake(40,100,20)]; [[self.tableView cellForRowAtIndexPath:indexPath].accessoryView addSubview:downloadButton]; [[self.tableView cellForRowAtIndexPath:indexPath].accessoryView setNeedsLayout]; [downloadButton release];
不幸的是,似乎没有做任何事情.我是否重新修复细胞校正?我需要添加另一种方式吗?
解决方法
尝试这个代码块而不是上面提供的块
UIButton *downloadButton = [UIButton buttonWithType:UIButtonTypeRoundedRect]; [downloadButton setTitle:@"Download" forState:UIControlStateNormal]; [downloadButton setFrame:CGRectMake(0,35)]; [tableView cellForRowAtIndexPath:indexPath].accessoryView = downloadButton;
这应该显示按钮,但是您仍然需要使用addTarget连接某些选择器. (我不知道如果在这个例子中听的附件ButtonTappedForRowWithIndexPath委托将工作,请先尝试,看看它是否按下按钮触发.)