2013-08-21 66 views
0

我想將UITableView中的所有UIButton viewwithTag :4000(以下代碼)添加到數組中。我怎樣才能做到這一點?請給我任何建議。 這是我的代碼創建的UITableView:如何將UITableView中的所有UIButton添加到IOS中的NSArray中

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

    NSString *identifier = @"identifier"; 
    cell = [_tableView dequeueReusableCellWithIdentifier:identifier]; 
    UIButton *market = [UIButton buttonWithType:UIButtonTypeCustom]; 
    if (cell == nil) { 
     cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:identifier] autorelease]; 
     [market setFrame:CGRectMake(200, 6, 30, 30)]; 
     market.tag = 4000; 

     [cell.contentView addSubview:market]; 

    } 

    UIButton *marketButton = (UIButton*)[cell.contentView viewWithTag:4000]; 
    [marketButton setTag:indexPath.row]; 
    if([sellingArray count]>0) 
    { 
     NSLog(@"sellingArray %@",sellingArray); 
     if([[sellingArray objectAtIndex:indexPath.row] isEqualToString:@"0"]) // nothing 
     { 
      [marketButton setImage:[UIImage imageNamed:@"Marketplace.png"] forState:UIControlStateNormal]; 

     } 
     else if([[sellingArray objectAtIndex:indexPath.row] isEqualToString:@"2"]) // marketplace 
     { 
      [marketButton setImage:[UIImage imageNamed:@"MarketplaceSelect.png"] forState:UIControlStateNormal]; 

     } 
    } 
    [marketButton setTag:indexPath.row]; 
    } 
    } 
return cell; 
} 

我想改變Marketbutton時reloadData的形象,但它不能改變,所以我想所有Marketbutton添加到陣列中,然後設置圖片爲每個按鈕。請幫助me.Thanks

回答

0
NSMutableArray* myButtons = [[NSMutableArray alloc] init]; 

... 

[myButtons addObject: marketButton]; 
+0

在哪裏你的代碼放?在cellForRowAtIndexPath? –

+0

NSMutableArray * myButtons = [[NSMutableArray alloc] init];在 - (無效)viewDidLoad ofc或在 - (id)initWithNibName如果你想 – 2013-08-21 10:43:43

+0

我試過你的代碼,但它顯示錯誤:[__NSArrayM insertObject:atIndex:]:對象不能爲零' –

0

在viewDidLoad中

myButtons = [[NSMutableArray alloc] init]; 

內如果條件

if(cell == nil){ 
... 
[myButtons addObject: marketButton]; 
} 
相關問題