繁体   English   中英

滚动视图或表视图标题中的UITable?

[英]UITable inside Scroll View or a Table View Header?

我目前正在构建一个应用,其中我将一些数据解析为UITableView 我正在使用自定义UITableViewCell类。

我想在UITableView之前和之上添加一个UIImage ,就像下面的UIImage一样。 我的问题是我是否应该UITableView一个包含UIImage的自定义UITableView标头视图? 或者是否应该将UITableViewUIImageView放在UIScrollView

我该怎么办?

当我滚动UITableView我希望顶部的UIImage消失,就像在Table View中是一行一样。

图片(John Mayer在Tour应用程序上:

约翰·梅耶(John Mayer)应用

我真的很感谢一些解决方案!

谢谢。

编辑:

这是完整的.m代码:

#import "DEMOSecondViewController.h"
#import "DEMONavigationController.h"
#import "PostsObject.h"
#import "RNBlurModalView.h"
#import "AFNetworking.h"
#import "PostsNextView.h"

#import "KIImagePager.h"
#import "TableHeaderView.h"

#import "DEMOMenuViewController.h"
#import "UIViewController+REFrostedViewController.h"

@interface DEMOSecondViewController ()  <KIImagePagerDelegate, KIImagePagerDataSource>
{
    IBOutlet KIImagePager *_imagePager;
}

@end

@implementation DEMOSecondViewController
@synthesize tableView = _tableView, activityIndicatorView = _activityIndicatorView;
@synthesize fontForCellText;
@synthesize btnFaceBook, btnTwitter, btnTwitter2;
@synthesize strURLToLoad;
@synthesize movies;

- (IBAction)showButtonMenu {
    [self.frostedViewController presentMenuViewController];
}

- (void)refresh:(UIRefreshControl *)refreshControl {
    [refreshControl endRefreshing];
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.

    UIRefreshControl *refreshControl = [[UIRefreshControl alloc] init];
    [refreshControl setTintColor:[UIColor greenColor]];
    [refreshControl addTarget:self action:@selector(refresh:) forControlEvents:UIControlEventValueChanged];
    [self.tableView addSubview:refreshControl];

    strURLToLoad = [[NSMutableString alloc] init];

    [btnFaceBook setTitle:@"link-1" forState:UIControlStateDisabled];
    [btnTwitter setTitle:@"link-2" forState:UIControlStateDisabled];
    [btnTwitter2 setTitle:@"link-3" forState:UIControlStateDisabled];

    [btnFaceBook setBackgroundImage:[UIImage imageNamed:@"tab_selected.png"] forState:UIControlStateNormal];
    [btnFaceBook setBackgroundImage:[UIImage imageNamed:@"tab_unselected.png"] forState:UIControlStateSelected];

    [btnTwitter setBackgroundImage:[UIImage imageNamed:@"tab_selected.png"] forState:UIControlStateNormal];
    [btnTwitter setBackgroundImage:[UIImage imageNamed:@"tab_unselected.png"] forState:UIControlStateSelected];

    [btnTwitter2 setBackgroundImage:[UIImage imageNamed:@"tab_selected.png"] forState:UIControlStateNormal];
    [btnTwitter2 setBackgroundImage:[UIImage imageNamed:@"tab_unselected.png"] forState:UIControlStateSelected];



    NSArray *nib = [[NSBundle mainBundle] loadNibNamed:@"PostsObject" owner:self options:nil];
    PostsObject *cell = [nib objectAtIndex:0];
    fontForCellText = cell.title.font;
    cellTextWidth = cell.title.frame.size.width;
    cellHeightExceptText = cell.frame.size.height - cell.title.frame.size.height;

    [self.navigationController setNavigationBarHidden:YES];

    self.tableView.separatorColor = [UIColor clearColor];

    // Setting Up Activity Indicator View
    self.activityIndicatorView = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhiteLarge];

    self.activityIndicatorView.color = [UIColor greenColor];


    self.activityIndicatorView.hidesWhenStopped = YES;
    self.activityIndicatorView.center = self.view.center;
    [self.view addSubview:self.activityIndicatorView];
    [self.activityIndicatorView startAnimating];
    self.tableView.separatorColor = [UIColor clearColor];

    // Initializing Data Source
    movies = [[NSMutableArray alloc] init];

    [self btnFromTabBarClicked:btnFaceBook];
}

-(UIStatusBarStyle)preferredStatusBarStyle{
    return UIStatusBarStyleLightContent;
}

- (void)loadJSONFromCurrentURL
{
    [self.activityIndicatorView startAnimating];

    NSURLRequest *request = [[NSURLRequest alloc] initWithURL:[NSURL URLWithString:strURLToLoad]];

    AFJSONRequestOperation *operation = [AFJSONRequestOperation JSONRequestOperationWithRequest:request success:^(NSURLRequest *request, NSHTTPURLResponse *response, id JSON) {
        [movies setArray:JSON];
        [self.activityIndicatorView stopAnimating];
        [self.tableView reloadData];

    } failure:^(NSURLRequest *request, NSHTTPURLResponse *response, NSError *error, id JSON) {
        NSLog(@"Request Failed with Error: %@, %@", error, error.userInfo);
    }];

    [operation start];
}

- (IBAction)btnFromTabBarClicked:(UIButton *)sender
{
    //Unselect all 3 buttons
    btnFaceBook.selected = btnTwitter.selected = btnTwitter2.selected = NO;

    //Select the button that was clicked
    sender.selected = YES;

    //Set the string of an NSMutableString property called strURLToLoad with the URL
    //The URL is pre stored in the text of the UIButton in the Disabled text.
    [strURLToLoad setString:[sender titleForState:UIControlStateDisabled]];

    //Load the URL
    [self loadJSONFromCurrentURL];
}

// How can I set an if-statement here? If the section is 0, return only 1 row, otherwise, the if (movies) function...

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    if (movies && movies.count) {
        return movies.count;
    } else {
        return 0;
    }
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 2;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    if (indexPath.section == 0) {
        static NSString *Identifier1 = @"TableHeaderView";
        // cell type 1
        TableHeaderView *cell = [tableView dequeueReusableCellWithIdentifier:Identifier1];
        if (cell == nil) {
            NSArray *nib= [[NSBundle mainBundle] loadNibNamed:@"TableHeaderView" owner:self options:nil];
            cell = (TableHeaderView *)[nib objectAtIndex:0];
        }

        //set the image on the cell

        return cell;
    } else {

        static NSString *Identifier2 = @"PostsObject";
        // cell type 2
        PostsObject *cell = [tableView dequeueReusableCellWithIdentifier:Identifier2];
        if (cell == nil) {
            NSArray *nib= [[NSBundle mainBundle] loadNibNamed:@"PostsObject" owner:self options:nil];
            cell = (PostsObject *)[nib objectAtIndex:0];
        }

        NSDictionary *movie = [self.movies objectAtIndex:indexPath.row];
        NSString *strText = [movie objectForKey:[self getTextKey]];

        CGRect rect = cell.title.frame;
        rect.size.height = [self getHeightForText:strText];
        cell.title.frame = rect;
        cell.title.text = strText;
        cell.arrow.center = CGPointMake(cell.arrow.frame.origin.x, rect.origin.y + rect.size.height/2);
        cell.published.text = [movie objectForKey:[self getPostedTime]];
        cell.twitterName.text = [movie objectForKey:[self getTwitterName]];
        return cell;
    }
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    [tableView deselectRowAtIndexPath:indexPath animated:YES];

    NSDictionary *selectedMovie = self.movies[indexPath.row];

    PostsNextView *nextVC = [[PostsNextView alloc] initWithDictionary:movies];

    [self.navigationController pushViewController:nextVC animated:YES];
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSDictionary *movie = [self.movies objectAtIndex:indexPath.row];
    NSString *strText = [movie objectForKey:[self getTextKey]];

    CGFloat cellHeight = cellHeightExceptText + [self getHeightForText:strText];

    return cellHeight;
}

- (NSString *)getTextKey
{
    return btnTwitter.selected?@"tweet":@"message";
}

- (NSString *)getPostedTime
{
    return btnTwitter.selected?@"posted":@"published";
}

- (NSString *)getTwitterName
{
    return btnTwitter2.selected?@"user":@"celebname";
}

- (CGFloat)getHeightForText:(NSString *)strText
{
    CGSize constraintSize = CGSizeMake(cellTextWidth, MAXFLOAT);
    CGSize labelSize = [strText sizeWithFont:fontForCellText constrainedToSize:constraintSize lineBreakMode:NSLineBreakByWordWrapping];
    NSLog(@"labelSize.height = %f",labelSize.height);
    return labelSize.height;
}


@end

你说自己回答了自己的问题

我希望顶部的图像消失,就像在“表格视图”中是一行一样

该图像应该只是表视图中的一行。 最好是设置不同的区域,图像将位于区域0,其余区域位于区域1。

您将要创建不同类型的单元格,一个已经用于突发新闻,另一个已经用于图像。

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    if (indexPath.section == 0) {
        static NSString *Identifier1 = @"CellType1";
        // cell type 1
        CellType1 *cell = [tableView dequeueReusableCellWithIdentifier:Identifier1];
        if (cell == nil) {
            NSArray *nib= [[NSBundle mainBundle] loadNibNamed:@"CellType1" owner:self options:nil];
            cell = (CellType1 *)[nib objectAtIndex:0];    
        }

        //set the image on the cell

        return cell;
    } else {
        static NSString *Identifier1 = @"CellType2";
        // cell type 2
        CellType2 *cell = [tableView dequeueReusableCellWithIdentifier:Identifier2];
        if (cell == nil) {
            NSArray *nib= [[NSBundle mainBundle] loadNibNamed:@"CellType2" owner:self options:nil];
            cell = (CellType2 *)[nib objectAtIndex:0];     
        }

        // set cell with breaking news stuff

        return cell;
    }
}

确保设置了节数

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 2;
}

还要确保为每种单元格类型使用不同的单元格标识符

并且在第一部分中仅获得一行,请确保相应地进行设置

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    if (section==0) {
        return 1;
    } else {
        return BREAKING_NEWS_COUNT;
    }
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM