我在tableview单元格上有按钮,这是以编程方式添加的,我在每个单元格上使用for循环添加了此按钮,我也设置了它" alpha值= 0"。我想要的是,当我点击特定的单元格时,它应该只显示该单元格上的按钮。
我在这做了什么:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath;
{
if(tableView == self.todayTableView)
{
for (int i = 0; i < first.count; i++)
{
record1 = [UIButton buttonWithType: UIButtonTypeRoundedRect];
[record1 setAlpha:0];
record1.frame = CGRectMake(250, 23, 25, 25);
[record1 addTarget:self action:@selector(record:) forControlEvents:UIControlEventTouchUpInside];
[record1 setImage:[UIImage imageNamed:@"microphone-g-ico.png"] forState:(UIControlStateNormal)];
[cell addSubview:record1];
}
[record1 setTag:indexPath.row];
return cell;
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
if(tableView == _todayTableView)
{
if(indexPath.row == 0)
{
if( record1.tag == 8)
{
[record1 setAlpha:1];
}
}
}
}
当我点击单元格时,它只显示最后一个单元格上的按钮。
plz暗示??
答案 0 :(得分:1)
更改此===&gt;
if(tableView == self.todayTableView)
{
for (int i = 0; i < first.count; i++)
{
record1 = [UIButton buttonWithType: UIButtonTypeRoundedRect];
[record1 setAlpha:0];
record1.frame = CGRectMake(250, 23, 25, 25);
[record1 addTarget:self action:@selector(record:) forControlEvents:UIControlEventTouchUpInside];
[cell addSubview:record1];
}
到==&gt;
if(tableView == self.todayTableView)
{
for (__strong UIView *view in Cell.contentView.subviews) {
if(view.tag==101){
[view removeFromSuperview];
view = nil;}
}
record1 = [UIButton buttonWithType: UIButtonTypeRoundedRect];
record1.tag=101;
[record1 setAlpha:0];
record1.frame = CGRectMake(250, 23, 25, 25);
[record1 addTarget:self action:@selector(record:) forControlEvents:UIControlEventTouchUpInside];
[record1 setImage:[UIImage imageNamed:@"microphone-g-ico.png"] forState:(UIControlStateNormal)];
[Cell.contentView addSubview:record1];
}
/ ********替换didSelect ******************** /
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
if(tableView == _todayTableView)
{
UITableViewCell *cell = [tableView
cellForRowAtIndexPath: indexPath];
for (__strong UIView *view in Cell.contentView.subviews) {
if(view.tag==101){
view.alpha=1;
break;
}
}
}
答案 1 :(得分:0)
我认为您应该重新加载tableview或重新加载单元格。您可以尝试以下代码:
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
// change Alpha button
if(tableView == _todayTableView) {
[cell.record1 setAlpha:0];
}
[tableView reloadRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationNone];
}
答案 2 :(得分:0)
无需使用for循环添加按钮,每次添加新行时都会调用cellForRowAtIndexPath:方法。
请尝试以下代码:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
if(tableView == self.todayTableView)
{
UIButton *record1 = [UIButton buttonWithType: UIButtonTypeRoundedRect];
[record1 setAlpha:0];
record1.frame = CGRectMake(250, 23, 25, 25);
[record1 addTarget:self action:@selector(record:) forControlEvents:UIControlEventTouchUpInside];
[record1 setImage:[UIImage imageNamed:@"microphone-g-ico.png"] forState:(UIControlStateNormal)];
[record1 setTag:indexPath.row];
}
return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
YourCellClass *cell = (YourCellClass*)[tableView cellForRowAtIndexPath:indexPath];
UIButton *record1 = (UIButton*)[cell viewWithTag:8];
if(tableView == _todayTableView)
{
if(indexPath.row == 0)
{
if( record1 != nil)
{
[record1 setAlpha:1];
}
}
}
}
答案 3 :(得分:0)
您似乎将record1
按钮存储在类的@property变量中。不要这样做,因为对于将要创建的每个单元格,新按钮将一遍又一遍地保存到同一个@property。所以最后,实际上只有最后创建的UIButton可供您使用,因为之前创建的所有UIButton都已被覆盖。
以下是您问题的解决方案:
1。)在- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {}
函数中,您应该为record1
- Button创建一个局部变量,如下所示:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
if(tableView == self.todayTableView) {
for (int i = 0; i < first.count; i++) {
// create NEW button, don't store it in a @property reference
UIButton* record1 = [UIButton buttonWithType:UIButtonTypeRoundedRect];
// now continue as you did
}
2.。)UITableViewCell中只有只有一个 UI-Element获取indexPath.row属性的标记非常重要。因为那样你可以通过它的标签属性访问UI-Element(在你的情况下是Button):
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
// get the UITableViewCell
UITableViewCell* cell = [tableView cellForRowAtIndexPath:indexPath];
// get the button and set its alpha property
UIButton* record1 = (UIButton*) [cell viewWithTag:indexPath.row];
record1.alpha = 1.0;
}
还有一个提示:第一个单元格将indexPath.row作为“0”值。这也可能导致问题,因为单元格中的每个UIView-Element默认都有一个tag = 0。因此,最好分配像indexPath.row + 100这样的标签。
答案 4 :(得分:0)
尝试一次
1.Create a mutable Array globally as
@interface YourClassName ()
{
NSMutableArray*selectedCellArr;
}
2.In ViewDidLoad Allocate Memory to that array as selectedCellArr = [NSMutableArray new];
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath;
{
if(tableView == self.todayTableView)
{
UIButton*record1 = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[record1 setAlpha:0];
record1.frame = CGRectMake(250, 23, 25, 25);
[record1 addTarget:self action:@selector(record:) forControlEvents:UIControlEventTouchUpInside];
[record1 setImage:[UIImage imageNamed:@"microphone-g-ico.png"] forState:(UIControlStateNormal)];
[record1 setTag:indexPath.row];
[cell addSubview:record1];
if ([selectedCellArr containsObject:indexPath]) [record1 setAlpha:1];
return cell;
}
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
if(tableView == _todayTableView)
{
if ([selectedCellArr containsObject:indexPath]) {
[selectedCellArr removeObject: indexPath];
}else{
[selectedCellArr addObject: indexPath];
}
}
}