2011-06-09 64 views
0

是否可以檢查uitableviewcellaccessoryview是否包含圖像?如何檢查uitableviewcell accessoryView是否包含圖像

例如

if([self.tableView cellForRowAtIndexPath:indexPath].accessoryView == [UIImage imageNamed:@"selected.png"]) 

一些此類似或不同的方法。
謝謝

+0

嘿@pooja你用多排在你的應用 – Robin 2011-06-09 10:32:01

+0

雅刪除實現多選的tableview與複選框中的每個細胞,你猜對了 – Pooja 2011-06-09 10:32:27

+0

不要繼電器,我不得不做很多的代碼更改,以獲得正確的東西 – Robin 2011-06-09 10:34:18

回答

0

對於我的應用我創建了一個自定義單元格,然後添加的ImageView的內容查看

但對於您的要求,你可以做這樣的事情

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 
    if (cell == nil) 
    { 
     cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease]; 
     UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 20, 20)]; 
     cell.accessoryView = imageView]; 
     imageView.tag = 3; 
     [imageView release]; 
    } 
    NSArray *subviews = [[NSArray alloc] initWithArray:cell.contentView.subviews]; 
    UIImageView *imageView; 
    for (UIView *subview in subviews) 
    { 
     if([subview isKindOfClass:[UIImageView class]] && subview.tag == 3); 
      imageView = [subview copy]; 
    } 
    [subviews release]; 

    if([selectedArray containsObject:indexPath]) 
     imageView.image = [UIImage imageNamed:@"Selected.png"]; 
    else 
     imageView.image = [UIImage imageNamed:@"Unselected.png"]; 
} 

並在此方法做同樣的事情來獲取imageView對象

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath 
1

默認情況下,accessoryView將爲null。

您可以檢查 -

if (self.tableView.accessoryView) { 

    // An image is present 
} 
0

第一:

if([self.tableView cellForRowAtIndexPath:indexPath].accessoryView == [UIImage imageNamed:@"selected.png"])

這是錯誤的... acessaryview不是一個圖像,它的UIView。

檢查:

if(nil != [self.tableView cellForRowAtIndexPath:indexPath].accessoryView) 
{ 
    // It has got something in it... 
} 
else 
{ 
//create an accessary view.... 
} 
0

我這是怎麼

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    static NSString *MyIdentifier = @"MyIdentifier"; 
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:MyIdentifier]; 
    UIButton *disclosureButton = [UIButton buttonWithType:UIButtonTypeCustom]; 
    [disclosureButton setFont:[UIFont fontWithName:@"Arial-BoldMT" size:12.0]]; 
    [disclosureButton setBackgroundImage:[UIImage imageNamed:@"checkboxoff.png"] forState:UIControlStateNormal]; 
    disclosureButton.tag = 0; 
    [disclosureButton setFrame:CGRectMake(0,0, 30,30)]; 
    [disclosureButton addTarget:self action:@selector(select:event:) forControlEvents:UIControlEventTouchUpInside]; 
    if (cell == nil) 
    { 
     cell = [[[UITableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier:MyIdentifier] autorelease]; 
     [cell setSelectionStyle:UITableViewCellSelectionStyleNone]; 
     [cell.textLabel setUserInteractionEnabled:YES]; 
     [cell setImage:[UIImage imageNamed:@"bluemappointer.png"]]; 
     [cell setAccessoryView:disclosureButton]; 
    } 
    else{ 
     int n = indexPath.row; 
     if([selectedPlaces objectForKey:indexPath] != nil){ 
      disclosureButton.tag = 1; 
      [disclosureButton setBackgroundImage:[UIImage imageNamed:@"checkboxon.png"] forState:UIControlStateNormal]; 
      [cell setAccessoryView:disclosureButton]; 
     } 
     else{ 
      [disclosureButton setBackgroundImage:[UIImage imageNamed:@"checkboxoff.png"] forState:UIControlStateNormal]; 
      disclosureButton.tag = 0; 
      [cell setAccessoryView:disclosureButton]; 
     } 
    } 

    NSString *name = [tableData objectAtIndex:indexPath.row]; 
    [cell.textLabel setText:name]; 

    return cell; 
}