在这个tableView单元格中,我创建了一个按钮,我希望在获得响应“1”后隐藏该按钮。我是按照以下方式进行的,但是仍然没有工作,单元格正在显示按钮。我会怎么做。
- (void)configureCell:(UITableViewCell *)cell atIndexPath:(NSIndexPath *)indexPath {
MyVideos *myvidoeclips = [park objectAtIndex:indexPath.row];
UILabel *lbl2=(UILabel*)[cell viewWithTag:777];
NSString *stateus = [myvidoeclips valueForKey:@"state"];
if([stateus isEqual: @"0"]){
[lbl2 setText:@"Under Moderation"];
}
if([stateus isEqual: @"1"]){
[lbl2 setText:@"Published"];
}
if([stateus isEqual: @"4"]){
[lbl2 setText:@"Rejected"];
}
lbl2.textColor=[UIColor whiteColor];
// Here is my button which i want to hide
UIButton *button=[[UIButton alloc]initWithFrame:CGRectMake(216, 130, 64, 30)];
NSString *strFlagValue = [[NSString alloc]init];
strFlagValue = [myvidoeclips valueForKey:@"state"];
if ([strFlagValue isEqualToString:@"1"])
{
flag = true;
}
else
{
flag = false;
}
if (flag)
{
button.hidden = true;
}
else
{
button.hidden = false;
}
[button addTarget:self action:@selector(yourEditButtonClicked:) forControlEvents:UIControlEventTouchUpInside]; button.tag = indexPath.row;
[button setImage:[UIImage imageNamed:@"btn-details.png"] forState:UIControlStateNormal];
[cell.contentView addSubview:button];
NSLog(@"sender.tag cell is%ld ",(long)button.tag);
}
答案 0 :(得分:1)
在cellForRowAtIndex检查标志。
标志将为真或假,具体取决于您的JSON响应(数组/字典)
BOOL flag;
NSString *strFlagValue = [[NSString alloc]init];
strFlagValue = [[array objectAtIndex:indexPath.row] valueForKey:@"Key_From_You_Get_1_Or_0"];
if ([strFlagValue isEqualToString:@"1"])
{
flag = true;
}
else
{
flag = false;
}
if (flag)
{
cell.button.hidden = true;
}
else
{
cell.button.hidden = false;
}
同时检查cellForRowAtIndex中的单元格是否必须可以使用标识符重复使用。
如果您确定每次都从JSON(数组)获得“1”或“0”。然后在排序代码下面就足够了。
BOOL flag = [[[array objectAtIndex:indexPath.row] valueForKey:@"Key_From_You_Get_1_Or_0"] boolValue];
cell.button.hidden = flag;
全cellForRowAtIndexPath(你用错误的方法在你的问题中写的逻辑)
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = @"ID_CustomCell";
CustomCell *cell = (CustomCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if(cell == nil){
cell = (CustomCell *)[[[NSBundle mainBundle] loadNibNamed:@"CustomCell" owner:self options:nil] objectAtIndex:0];
cell.selectionStyle = UITableViewCellSelectionStyleNone;
}
BOOL flag = [[[array objectAtIndex:indexPath.row] valueForKey:@"Key_From_You_Get_1_Or_0"] boolValue];
cell.btn1.hidden = flag;
NSString *strFlagValue = [[NSString alloc]init];
strFlagValue = [[array objectAtIndex:indexPath.row] valueForKey:@"Key_From_You_Get_1_Or_0"];
if ([strFlagValue isEqualToString:@"1"])
{
flag = true;
}
else
{
flag = false;
}
if (flag)
{
cell.btn1.hidden = 1;
}
else
{
cell.btn1.hidden = 0;
}
return cell;
}
答案 1 :(得分:0)
对于静态表格单元格,创建一个UIButton的IBOutlet并在使用json数据检查条件时隐藏。
对于动态表格单元格,需要识别需要隐藏按钮的行号。
示例: - 有一个包含多个字典的数组。
NSDictionary* dict = [array objectAtIndex:indexPath.row];
BOOL isButtonHidden = [dict objectForKey:@"KeyName"];
cell.button.hidden = isButtonHidden
希望这个解决方案可以帮助你......谢谢