iphone - 如何使除选定一个以外的所有表格 View 单元格变灰

标签 iphone ios uitableview custom-cell

我有一个带有自定义单元格的表格 View ,自定义单元格上有 uibuttons,如果我选择按钮,除了单元格剩余的所有单元格都应该变灰或禁用是可能的。

//tableview 类中的代码

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section;
{
NSLog(@"No OF rows:%d",[contents count]);
return [contents count];

}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath;
{

static NSString *cellIdentifier = @"cell";

// Try to retrieve from the table view a now-unused cell with the given identifier.
cell = (uploadCustomCell *)[tableView    dequeueReusableCellWithIdentifier:@"uploadCustomCell"];
if (cell == nil) {
    NSLog(@"cell allocated");
    // Use the default cell style.

    cell = [[uploadCustomCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"uploadCustomCell"];
    NSArray *nib = [[NSBundle mainBundle] loadNibNamed:@"uploadCustomCell"
                                                        owner:self options:nil];


  cell = [nib objectAtIndex:0];
}
   saveBtnCcell.hidden = YES;
   cell.textNamefield.hidden = YES;
 [cell setSelectionStyle:UITableViewCellSelectionStyleNone];
[cell.defaultSwitch setEnabled:NO];
   dictionaryContents = [contents objectAtIndex:indexPath.row];
   NSLog(@"dict dict :%@",dictionaryContents);
  //

   cell
.nameLabelCell.text   = [dictionaryContents valueForKey:@"VideoName"];
    cell.userName.text = [dictionaryContents valueForKey:@"User"];
  NSLog(@"Array Image:%@",arrayimage);
  cell.thumbImg.image = [arrayimage objectAtIndex:indexPath.row];
   NSLog(@"ARimage:%@,index%d",[arrayimage objectAtIndex:indexPath.row],indexPath.row);
   NSString *defaultVideo = [dictionaryContents valueForKey:@"DefaultVideo"];
NSLog(@"Default Video:%@",defaultVideo);
if ([defaultVideo isEqual: @"1"]) {
    //        [cell.defaultSwitch setOn:YES animated:YES];
    [defaultSwitche setOn:YES animated:YES];

}
else{
      //        [cell.defaultSwitch setOn:NO animated:YES];
    [defaultSwitche setOn:NO animated:YES];
}

   [cell.defaultSwitch addTarget:self action:@selector(setState:) forControlEvents:UIControlEventValueChanged];



VideoNameTextField.hidden = YES;

   return cell;


}

//自定义单元格中的代码
  @interface uploadCustomCell (){
UploadAllViewController *uploadAll;
}

@end
@implementation uploadCustomCell
@synthesize textNamefield;
@synthesize savebtn,edit,nameLabelCell,textLabel,uploadBTN;
@synthesize defaultSwitch;
//@synthesize uploadAll;
- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
if (self) {
    // Initialization code
}
return self;
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated
{
[super setSelected:selected animated:animated];

// Configure the view for the selected state
}


- (void)dealloc {
[_userName release];

[_thumbImg release];

  //[savebtn release];
[textNamefield release];
[nameLabelCell release];
[_test release];

[savebtn release];
[defaultSwitch release];
[uploadBTN release];
[super dealloc];
}


- (IBAction)editAction:(id)sender {
[uploadBTN setEnabled:NO];
uploadAll = [[UploadAllViewController alloc]init];
CGPoint buttonPosition = [sender convertPoint:CGPointZero toView:uploadAll.tabelView1];
NSIndexPath *indexPath = [uploadAll.tabelView1 indexPathForRowAtPoint:buttonPosition];
int no = indexPath.row;
NSLog(@"index path :%d",no);
[uploadAll didEditButtonPressed:self];

}
- (IBAction)saveBtnAction:(id)sender {
 [uploadBTN setEnabled:YES];
[uploadAll didSaveButtonPressed:self];
}

当我选择这个editAction时:除了单元格剩余的单元格应该是灰色的。

最佳答案

在您的 cellForRowAtIndexPath您必须考虑表格 View 的状态,即是否选择了一个或零个单元格。使用它来根据需要更改单元格的外观。在下面的例子中,我假设你有一个没有任何部分的直线数组,但是同样的原理也适用于 indexPath。 s 也是。我使用 int selectedRow设置为 -1如果没有选择单元格。

#define kNoCellSelected -1

// in cellForRowAtIndexPath:

if (self.selectedRow == kNoCellSelected) {
    cell.backgroundView.backgroundColor = normalColor;
    cell.userInteractionEnabled = YES;
}
else if (self.selectedRow != indexPath.row) {
    cell.backgroundView.backgroundColor = disabledColor;
    cell.userInteractionEnabled = NO;
}

不要忘记设置selectedRowdidSelectRowAtIndexPath:viewDidLoad .

关于iphone - 如何使除选定一个以外的所有表格 View 单元格变灰,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16462896/

相关文章:

iphone - 在 native iPhone 应用程序中重复背景图像

iphone - 如何从 iOS 上的 AudioQueue 服务获取样本

objective-c - 是否可以从 NSData 或其他图像类型(CFImageRef、CIImage、UIImage)创建 ALAsset 对象?

iphone - 创建数据的自定义格式文件并实现以应用程序格式导出功能并在应用程序中打开文件

ios - Cordova ios 添加扩展 - 错误 : could not find -Info. plist 文件或 config.xml 文件

ios - 在 TableView 的自定义单元格中隐藏/显示标签

iphone - UITableViewCell 的 UITextField subview 成为第一响应者?

javascript - Android 或 Iphone 中存储的 phonegap 数据库在哪里?

ios - 使用 CoreData 保存用户文章 - 如何让所有文章都保存在 UITableView 中?

ios - 向选定的表格单元格添加复选标记还会检查另一个表格单元格