2013-05-09 179 views
1

我有自定義單元格的表格視圖,自定義單元格中有uibuttons,如果我選擇按鈕除了單元格保持所有單元格都應該變灰或禁用,這是可能的。如何格式化除選定的單元格外的所有tableview單元格

//代碼中的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; 


} 

//代碼在customcell

@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:除了電池剩餘細胞應當grayouted。

+0

嗨,歡迎來到SO。爲了能夠幫助你,你需要提供更多的信息和代碼。閱讀http://stackoverflow.com/faq#questions,瞭解您可以在此處詢問哪些類型的問題。 – 2013-05-09 13:32:06

回答

2

在您的cellForRowAtIndexPath中,您必須考慮表格視圖的狀態,即選擇了一個或零個單元格。根據需要使用它來改變單元格的外觀。在下面的例子中,我假定你有一個沒有任何部分的直線陣列,但是同樣的原理也適用於indexPath。如果沒有選定單元格,我使用設置爲-1intselectedRow

#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

+0

如果我們在indexpath的行中使用此代碼行,我們不會每次按按鈕時重新加載表。 – KMKR 2013-05-10 14:02:13

+0

這不是這個代碼所做的。您必須在您的按鈕方法中重新加載「運行索引路徑」。但是,只有像上面那樣動態配置每個單元時,它纔會起作用。 – Mundi 2013-05-11 09:41:14