簡體   English   中英

如何在UITableview iOS的每一行中啟用/禁用

[英]How to enable/disable in each row of UITableview iOS

我需要在UItableview的每一行中禁用/啟用uibutton,我比較2個NSMutablearray來啟用/禁用它。我嘗試下面的代碼,但它總是在第一行中啟用按鈕。 我的代碼哪里有錯誤?

 - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSString *simpleTableIdentifier = [NSString stringWithFormat:@"%d,%d",indexPath.section,indexPath.row];

    UITableViewCell *cell= [_tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier];


    UIButton *pausebtn= [UIButton buttonWithType:UIButtonTypeCustom];

    if (cell == nil) 
    {
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:simpleTableIdentifier] autorelease];
        [pausebtn setFrame:CGRectMake(285, 10, 30,30)];
        [pausebtn setTag:4000];
        [pausebtn setImage:[UIImage imageNamed:@"Pause.png"] forState:UIControlStateNormal];
        [pausebtn addTarget:self action:@selector(pausebtnClicked:event:) forControlEvents:UIControlEventTouchDown];
        [cell.contentView addSubview:pausebtn];

    }

     pausebtn = (UIButton*)[cell.contentView viewWithTag:4000];
    [pausebtn addTarget:self action:@selector(pausebtnClicked:event:) forControlEvents:UIControlEventTouchDown];
    [pausebtn setImage:[UIImage imageNamed:@"Pause.png"] forState:UIControlStateNormal];

   //check 2 arrays

   if ([[Array1 objectAtIndex:indexPath.row] integerValue] != [[Array2 objectAtIndex:indexPath.row] integerValue]) 
   {
        pausebtn.enabled = YES;
   }
   else{
       pausebtn.enabled = NO;
   }


   return cell;
}

謝謝你的幫助。

在檢查單元格是否為零並創建它之前,聲明您的UIButton。 僅當單元格為nill時才創建UIButton如果單元格已存在,則按標記獲取它。

UIButton *pausebtn;

if (cell == nil) {
    cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:simpleTableIdentifier] autorelease];
    pausebtn= [UIButton buttonWithType:UIButtonTypeCustom];
    .....

}
    pausebtn = (UIButton*)[cell.contentView viewWithTag:4000];
    .....

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM