【问题标题】:Logic for adding images and scroll icon to the table view将图像和滚动图标添加到表格视图的逻辑
【发布时间】:2011-06-09 05:49:24
【问题描述】:

我有一个表格视图,我将四个UIImageViews 添加到一行,所以我总共有五行和二十个图像视图。我正在将图像一张一张地插入到单元格中。只有在所有四个图像视图都被填充后,第二行才会开始填充。

我需要一些逻辑。我的意思是,我如何检查表格视图中接下来要填充的位置以及在哪个事件中?如何将图像一张一张添加到UIImageViews?此外,最初只会显示两行。填完这两行后,当图像开始进入第三行时,我需要在屏幕上的最后一行旁边显示一个图标,告诉用户下面还有更多数据。另外,是否有任何事件可以检测当前屏幕上的最后一行是哪一行?

还有什么方法可以从一行中单独选择图像?

【问题讨论】:

    标签: objective-c cocoa-touch ios uitableview uiscrollview


    【解决方案1】:

    运行此代码:-

    .h file code
    
    
    #import <UIKit/UIKit.h>
    
    @interface helpTableViewController : UIViewController {
        IBOutlet UITableView *gridTableView;
        NSMutableArray *imagesArray;
        int widthview,widthview1;
    
        }
    @property(nonatomic,retain)NSMutableArray *imagesArray;
    @property(nonatomic,retain)UITableView *gridTableView;
    -(UITableViewCell *)reuseTableViewCellWithIdentifier:(NSString *)identifier withIndexPath:(NSIndexPath *)indexPath;
    -(void)crossViewButtonClicked:(UIButton *)sender;
    
    @end
    

    .m 文件代码:-

    @synthesize gridTableView;
    @synthesize imagesArray;
    
    
    // Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
    - (void)viewDidLoad {
        [super viewDidLoad];
        NSMutableArray *tempArray=[[NSMutableArray alloc]init];
        self.imagesArray=tempArray;
        [tempArray release];
        [self.imagesArray addObject:[UIImage imageNamed:@"pic1.png"]];
        [self.imagesArray addObject:[UIImage imageNamed:@"pic2.png"]];
        [self.imagesArray addObject:[UIImage imageNamed:@"pic3.png"]];
        [self.imagesArray addObject:[UIImage imageNamed:@"pic4.png"]];
        [self.imagesArray addObject:[UIImage imageNamed:@"pic5.png"]];
        [self.imagesArray addObject:[UIImage imageNamed:@"pic6.png"]];
        [self.imagesArray addObject:[UIImage imageNamed:@"pic1.png"]];
        [self.imagesArray addObject:[UIImage imageNamed:@"pic2.png"]];
        [self.imagesArray addObject:[UIImage imageNamed:@"pic3.png"]];
        widthview=0;
        widthview1=0;
    }
    
    - (void)viewDidUnload {
        self.gridTableView=nil;
        // Release any retained subviews of the main view.
        // e.g. self.myOutlet = nil;
    }
    
    
    - (void)dealloc {
        [imagesArray release];
        [gridTableView release];
        [super dealloc];
    }
    
    
    #pragma mark -
    #pragma mark TableView Methods
    
    - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
        return 1;
    }
    
    
    // Customize the number of rows in the table view.
    - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
        return 2;
    
    }
    
    
    - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
        static NSString *CellIdentifier = @"Cell";
    
        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
        cell=nil;
        if (cell == nil) {
            cell =[self reuseTableViewCellWithIdentifier:CellIdentifier withIndexPath:indexPath];
    
        }
    
                return cell;
    }
    -(UITableViewCell *)reuseTableViewCellWithIdentifier:(NSString *)identifier withIndexPath:(NSIndexPath *)indexPath
    {
        UITableViewCell *cell =[[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault  reuseIdentifier:identifier]autorelease];
        if (indexPath.row==0) {
            UIScrollView *scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, 320, 180)];
            scrollView.contentSize = CGSizeMake(320, 180);
            scrollView.backgroundColor=[UIColor clearColor];
            scrollView.showsHorizontalScrollIndicator = NO;
            [cell.contentView addSubview:scrollView];       
            for (int i=0; i<4;i++) {        
                UIView *profileView=[[UIView alloc]initWithFrame:CGRectMake(widthview+10, 12, 95, 155)];
                profileView.backgroundColor=[UIColor whiteColor];
                UIImageView *profileimageView=[[UIImageView alloc]initWithFrame:CGRectMake(5, 8, 83, 106)];
                [profileimageView setImage:[self.imagesArray objectAtIndex:i]];
                profileimageView.tag=i+90;
                [profileView addSubview:profileimageView];
                [profileimageView release];
                [scrollView addSubview:profileView];
                widthview=widthview+105;
                NSLog(@"%d",widthview);
            }
            UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
            [button setFrame:CGRectMake(widthview1+10, 12, 95, 155)];
            //button.contentMode = UIViewContentModeScaleAspectFill;
            button.tag=999999;
            [button setBackgroundColor:[UIColor greenColor]];
            [button addTarget:self action:@selector(imageButtonClicked:) forControlEvents:UIControlEventTouchUpInside];
            [scrollView addSubview:button];
            widthview=widthview+105;
            scrollView.contentSize = CGSizeMake(widthview, 180);
    
        }
        if (indexPath.row==1) {
            UIScrollView *scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, 320, 180)];
            scrollView.contentSize = CGSizeMake(320, 180);
            scrollView.backgroundColor=[UIColor clearColor];
            scrollView.showsHorizontalScrollIndicator = NO;
            [cell.contentView addSubview:scrollView];       
            for (int i=0; i<4;i++) {        
                UIView *profileView=[[UIView alloc]initWithFrame:CGRectMake(widthview1+10, 12, 95, 155)];
                profileView.backgroundColor=[UIColor whiteColor];
    
                UIImageView *profileimageView=[[UIImageView alloc]initWithFrame:CGRectMake(5, 8, 83, 106)];
                //  [profileimageView setImage:[UIImage imageNamed:@"gridimage1.png"]];
                profileimageView.tag=4+i;
                [profileimageView setImage:[self.imagesArray objectAtIndex:3+i]];
                [profileView addSubview:profileimageView];
    
                [scrollView addSubview:profileView];
                [profileimageView release];
                widthview1=widthview1+105;
                NSLog(@"%d",widthview1);
            }
            scrollView.contentSize = CGSizeMake(widthview1, 180);
    
        }return cell;
    
    }
    
    // Override to support row selection in the table view.
    - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    
    }
    #pragma mark -
    #pragma mark button methods
    
    
    -(void)imageButtonClicked:(UIButton *)sender
    {
        [self.imagesArray replaceObjectAtIndex:0 withObject:[UIImage imageNamed:@"abdulprofilepic.png"]];
        [self.gridTableView reloadData];
    
    }
    

    【讨论】:

    • 朋友,当我点击按钮时,我的应用程序因此代码而崩溃
    • 你能告诉我按钮名称应该是什么吗?对不起,我是这个问题的新手
    • 我在我的屏幕上看不到任何东西,无论是 tabke 视图还是按钮
    • 你有没有在你的视图中放置一个表格视图,将表格行高添加到 180.den 尝试
    • 崩溃可能是由于 self.images 数组。这是包含图像的数组。让您的数组在其中添加图像。
    【解决方案2】:

    您应该将所有逻辑(和数据)保留在表视图控制器(UITableViewController 子类)中。我建议你有一个自定义的UITableViewCell 子类,它可以容纳 4 张图像(单元格本身应该负责布局),表格视图控制器负责设置它。在-tableView:cellForRowAtIndexPath: 中,您可以查看您的图像集合,然后只需使用modular arithmetic 来确定该特定索引路径的单元格中应该包含哪些图像。

    至于单独选择图像,也许您可​​以让单元格知道它们的索引路径(在-tableView:cellForRowAtIndexPath: 中设置为自定义属性),然后每个单元格都有一个委托方法告诉您的控制器按下了某个图像(例如, -cellAtIndexPath:didSelectImageAtIndex:)。

    【讨论】:

      【解决方案3】:

      试试这个, 由于您要在 i 行中显示固定数量的图像。 e. 4 所以你要做的是: - 在 .h 文件中

      int widthview,widthview1;
      

      在 viewdidload 中的 .m 文件中

         widthview=0;
          widthview1=0;
      
      
      
      - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
      
          static NSString *CellIdentifier = @"Cell";
      
          UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
          if (cell == nil) {
             cell = [self reuseTableViewCellWithIdentifier:CellIdentifier withIndexPath:indexPath];
      
          }
          cell.selectionStyle = UITableViewCellSelectionStyleNone;//to remove blu selection color
          return cell;
      }
      -(UITableViewCell *)reuseTableViewCellWithIdentifier:(NSString *)identifier withIndexPath:(NSIndexPath *)indexPath
      {
          UITableViewCell *cell =[[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault  reuseIdentifier:identifier]autorelease];
          if (indexPath.row==0) {
              UIScrollView *scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, 320, 180)];
              scrollView.contentSize = CGSizeMake(320, 180);
              scrollView.backgroundColor=[UIColor clearColor];
              scrollView.showsHorizontalScrollIndicator = NO;
              [cell.contentView addSubview:scrollView];       
              for (int i=0; i<4;i++) {        
                  UIImageView *profileimageView=[[UIImageView alloc]initWithFrame:CGRectMake(widthview+10, 12, 95, 155)];
                  [profileimageView setImage:[self.imagesArray objectAtIndex:i]];
                  [profileimageView release];
                  [scrollView addSubview:profileimageView];
                  widthview=widthview+105;
                  NSLog(@"%d",widthview);
              }
              scrollView.contentSize = CGSizeMake(widthview, 180);
      
          }
          if (indexPath.row==1) {
              UIScrollView *scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, 320, 180)];
              scrollView.contentSize = CGSizeMake(320, 180);
              scrollView.backgroundColor=[UIColor clearColor];
              scrollView.showsHorizontalScrollIndicator = NO;
              [cell.contentView addSubview:scrollView];       
              for (int i=0; i<4;i++) {        
                  UIImageView *profileimageView=[[UIImageView alloc]initWithFrame:CGRectMake(widthview+10, 12, 95, 155)];
                  [profileimageView setImage:[self.imagesArray objectAtIndex:4+i]];
                  [profileimageView release];
                  [scrollView addSubview:profileimageView];
                  widthview1=widthview1+105;
                  NSLog(@"%d",widthview1);
          }
              scrollView.contentSize = CGSizeMake(widthview1, 180);
      
          }
      return cell;
      
      }
      

      此代码适用于 2 行,对接下来的 3 行执行相同的操作。 根据您的要求调整坐标。 希望这会节省您的时间。

      【讨论】:

      • 这段代码能帮我一张一张添加图片吗..到单元格中的图片视图
      • @Christina 是的,它会尝试这个我也使用此代码在表格视图单元格的滚动视图中添加图像
      • 一旦我尝试这个,我会通知你好的,但是我想在单击按钮时添加图像,例如当我单击它时,第一个图像会在桌面视图上播放,然后我再次单击在按钮上然后显示下一个图像等等。我怎么能做到这一点,要调用哪些事件
      • @christina 你的按钮在哪里
      • @christina 你能说出你想要做什么的确切场景吗?
      【解决方案4】:

      回答还有没有办法从一行中单独选择图像? : 将图像赋予按钮并将该按钮添加到行内!

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2018-12-02
        • 1970-01-01
        • 2021-06-05
        • 1970-01-01
        相关资源
        最近更新 更多