ios - 在页脚按钮上单击表格 View 不会重新加载表格的内容?

标签 ios objective-c uiimageview tableview

在表格 View 页脚上我有一个按钮。单击页脚按钮将重新加载表格数据。但是当数据重新加载时,表的数据再次显示,其中我有 cameraBtnUIImagePicker View Controller 中选择图像并将其显示在 ImageView 中。但我希望在重新加载表格时无法显示表格的图像。任何人都可以帮助我解决这个问题。我正在使用以下代码。

.h文件

{
    BOOL isBtnClicked; // in .h file
}

.m 文件

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"Cell";

    UITableViewCell *cell = [createDelvTableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil)
    {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
    }

    NSArray *subviewArray = cell.contentView.subviews;
    for (UIView *view in subviewArray)
    {
        [view removeFromSuperview];
    } 

    UILabel *picLabels = [[UILabel alloc]initWithFrame:CGRectMake(100, 10, 150, 60)];
    picLabels.text = listItems[indexPath.row];
    picLabels.textColor = [UIColor colorWithRed:74.0/255.0 green:144.0/255.0 blue:226.0/255.0 alpha:1];
     picLabels.font =[UIFont systemFontOfSize:13];
    [cell.contentView addSubview:picLabels];

    if (isBtnClicked )
    {
        imageParcel =[[UIImageView alloc] initWithFrame:CGRectMake(5, 10, 60,60)];
        imageParcel.userInteractionEnabled=YES;
        imageParcel.backgroundColor=[UIColor groupTableViewBackgroundColor];
        imageParcel.layer.cornerRadius = 30;
        imageParcel.layer.masksToBounds = YES;
        [cell.contentView addSubview:imageParcel];
    }
    else
    {
        imageParcel =[[UIImageView alloc] initWithFrame:CGRectMake(5, 10, 60,60)];
        imageParcel.userInteractionEnabled=YES;
        imageParcel.backgroundColor=[UIColor groupTableViewBackgroundColor];
        imageParcel.image = [imageArray objectAtIndex:indexPath.row];
        imageParcel.layer.cornerRadius = 30;
        imageParcel.layer.masksToBounds = YES;
        [cell.contentView addSubview:imageParcel];
    }

    parcelButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
    parcelButton.frame = CGRectMake(180, 0, 50, 50);
    [parcelButton setBackgroundImage:[UIImage imageNamed:@"Camera.png"]forState:UIControlStateNormal];
    parcelButton.tag = indexPath.row;
    [parcelButton addTarget:self action:@selector(addPictureAction:) forControlEvents:UIControlEventTouchUpInside];
    cell.accessoryType = UITableViewCellAccessoryNone;
    cell.accessoryView = parcelButton;

    return cell;
}

- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section
{
    mainview = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 0, 0)];
    [mainview setBackgroundColor:[UIColor groupTableViewBackgroundColor]];
    createDelvTableView.tableFooterView = mainview;

    UIButton *addSection = [[UIButton alloc]initWithFrame:CGRectMake(270, 0, 40, 40)];
    isBtnClicked = YES;
    [addSection setBackgroundImage:[UIImage imageNamed:@"addSection.png"] forState:UIControlStateNormal];
    [addSection addTarget:self action:@selector(addSectionAction) forControlEvents:UIControlEventTouchUpInside];
    [mainview addSubview:addSection];

    return mainview;
}

- (void)addPictureAction:(UIButton*)image
{
    NSLog(@"Add Pictures");        
    isBtnClicked = NO;        
    index=image.tag;        
    // if ([UIImagePickerController isSourceTypeAvailable : UIImagePickerControllerSourceTypeCamera])  {
    UIImagePickerController *photoPicker = [[UIImagePickerController alloc] init];
    photoPicker.delegate = self;
    photoPicker.allowsEditing = YES;
    photoPicker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
    [self presentViewController:photoPicker animated:YES completion:NULL];

    UIAlertView *myAlertView = [[UIAlertView alloc] initWithTitle:@"Error"
                                                          message:@"Device has no camera"
                                                         delegate:nil
                                                cancelButtonTitle:@"OK"
                                                otherButtonTitles: nil];
    [myAlertView show];        
}
- (void)addSectionAction
{
    NSLog(@"ADD section");
    indexes++;
    isBtnClicked = YES;
    [createDelvTableView reloadData];        
}

提前致谢。

最佳答案

我不明白为什么你一次又一次地在单元格上添加对象..然后删除它们..修改你的cellForRowAtIndexPath

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"Cell";

UITableViewCell *cell = [createDelvTableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil)
{
    cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];

    UILabel *picLabels = [[UILabel alloc]initWithFrame:CGRectMake(100, 10, 150, 60)]; 
    picLabels.textColor = [UIColor colorWithRed:74.0/255.0 green:144.0/255.0 blue:226.0/255.0 alpha:1];
    picLabels.font =[UIFont systemFontOfSize:13];
    picLabels.tag = 101;
    [cell.contentView addSubview:picLabels];

    UIImageView *imageParcel =[[UIImageView alloc] initWithFrame:CGRectMake(5, 10, 60,60)];
    imageParcel.tag = 102;
    imageParcel.userInteractionEnabled=YES;
    imageParcel.backgroundColor=[UIColor groupTableViewBackgroundColor];
    imageParcel.layer.cornerRadius = 30;
    imageParcel.layer.masksToBounds = YES;
    [cell.contentView addSubview:imageParcel];


    UIButton *parcelButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
    parcelButton.frame = CGRectMake(180, 0, 50, 50);
   [parcelButton setBackgroundImage:[UIImage imageNamed:@"Camera.png"]forState:UIControlStateNormal];
   parcelButton.tag = indexPath.row;
   [parcelButton addTarget:self action:@selector(addPictureAction:) forControlEvents:UIControlEventTouchUpInside];

}
UILabel *lbl = (UILabel*)[cell.contentView viewWithTag:101];
picLabels.text = listItems[indexPath.row];

UIImageView *img = (UIImageView*)[cell.contentView viewWithTag:102];
if (!isBtnClicked )
{
    imageParcel.image = [UIImage imageNamed:[imageArray objectAtIndex:indexPath.row]];
}
else
{
   imageParcel.image = [UIImage new];
}
cell.accessoryType = UITableViewCellAccessoryNone;
cell.accessoryView = parcelButton;

return cell;
}

viewForFooterInSection 中删除 isBtnClicked = YES;

在页脚的按钮操作上:

isBtnClicked = YES;
[yourTblView reloadData];

在 ImagePicker 按钮操作上:-

isBtnClicked = NO;

关于ios - 在页脚按钮上单击表格 View 不会重新加载表格的内容?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31579493/

相关文章:

objective-c - 点击时如何放大 UIImageView

ios - 缩放时 UIScrollView subview 居中

ios - 快速添加 UICollectionView 代替导航栏标题

ios - MKStoreKit 在 iOS 7 上崩溃并出现 SIGTRAP 错误

ios - Cordova & CORS (iOS)

iphone - 使用 objective-c 将文件从应用程序上传到服务器

ios - 获取表情符号键盘IOS 8的高度

ios - 在removeFromSuperview()之后UIView w/animation没有被删除

ios - 如果 App Store iOS 中有新版本的应用程序可用,是否有适当的方法在应用程序内通知用户?

ios - 在 uiimageview 的底部中间添加三角形,就像在 swift 中的标记一样