0

我有带指示器的tableView,但是当我单击按钮时,它会显示小弹出窗口图像,如果单击的单元格位于顶部,则我希望该图像应与单元格对齐,然后如果单击的单元格为middel,则弹出窗口应显示在顶部。

现在我已经固定在屏幕上的一个位置。

当您按下任何单元格按钮 addtoFave 然后弹出按钮与该单元格对齐时,我想像普通的 ipad 应用程序一样制作任何想法如何完成此操作。

这是我当前的代码:

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

    NSLog(@"ContentID %@",contentID);

    CustomCell *cell  = (CustomCell *) [tblSimpleTable cellForRowAtIndexPath:indexPath];

    UIImageView *imgView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 312, 105)];
    imgView.frame = CGRectMake((cell.frame.size.width/2)-(imgView.frame.size.width/2),  (cell.frame.size.height/2)-(imgView.frame.size.height/2), 312, 105);

    imgView.image=[UIImage imageNamed:@"popupj.png"];

    [cell addSubview:imgView];

}   

但是,当我单击其他单元格时,这显示单元格上的图像并不是全部,当我单击其他单元格时,此图像仅显示在第一个单元格上,我希望选择的单元格应该显示给该单元格。

4

3 回答 3

5

首先添加这一行myImageView.center = cell.center;并添加你myImageViewcell.contentView

或者

您也可以将此用于显示图像的代码放在UITableViewCell.

myImageView.center = CGPointMake(cell.contentView.bounds.size.width/2,cell.contentView.bounds.size.height/2);
于 2013-07-04T06:51:58.860 回答
1

//如果你想在中心的任何视图上显示任何子视图..试试这个..从下面选择选项1

// 选项1:

   UIImageView *imgView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 50, 50)];
   imgView.frame = CGRectMake((cell.frame.size.width/2)-(imgView.frame.size.width/2), (cell.frame.size.height/2)-(imgView.frame.size.height/2), 50, 50);
   [cell addSubview:imgView];

或者

// 选项 2:

// 在.h

int selectedRow;

// 在.m

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.row == selectedRow)
    {
       UIImageView *imgView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 50, 50)];
       imgView.frame = CGRectMake((cell.frame.size.width/2)-(imgView.frame.size.width/2), (cell.frame.size.height/2)-(imgView.frame.size.height/2), 50, 50);
       imgView.image = [UIImage imageNamed:@"Select.png"];
       [cell addSubview:imgView];
       [imgView release];
       imgView = nil;
    }
    else
    {
       UIImageView *imgView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 50, 50)];
       imgView.frame = CGRectMake((cell.frame.size.width/2)-(imgView.frame.size.width/2), (cell.frame.size.height/2)-(imgView.frame.size.height/2), 50, 50);
       imgView.image = [UIImage imageNamed:@"UnSelect.png"];
       [cell addSubview:imgView];
       [imgView release];
       imgView = nil;

    }
}


- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
     selectedRow = indexPath.row;
     [tableview reloadData];
}
于 2013-07-04T07:14:31.647 回答
1

这就是我使用自定义 UITableViewCell 使 UIImageView 居中的方式。

请务必将此代码添加到 UITableViewCell 类的 override func layoutSubviews() {} 部分。

    var cellHeight:CGFloat = 150
    self.imageLogo.frame = CGRectMake(0, 0, 125, 125)
    self.imageLogo.center = CGPoint(x: self.center.x, y: cellHeight / 2)
    self.imageLogo.image = Common.school_crest_image
    self.imageLogo.contentMode = UIViewContentMode.ScaleAspectFit
    self.addSubview(self.imageLogo)
于 2015-06-04T03:53:49.113 回答