【问题标题】:UITable inside Scroll View or a Table View Header?滚动视图中的 UITable 还是表视图标题?
【发布时间】:2014-01-31 19:46:15
【问题描述】:

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

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

我该怎么做?

当我滚动 UITableView 时,我希望顶部的 UIImage 消失,就像它是表格视图中的一行一样。

图片(John Mayer on Tour 应用程序:

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

谢谢。

编辑:

这是完整的 .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

【问题讨论】:

  • 是否使用情节提要?
  • 不。没有故事板。 @JPHribovsek

标签: ios objective-c uitableview uiimage


【解决方案1】:

当你说

时,你回答了你自己的问题

我希望顶部的图像消失,就像它是 表格视图

图像应该只是表格视图中的一行。 最好的可能是设置不同的部分,图像在 #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;
    }
}

【讨论】:

  • 好的,它有效。只有一个问题。如何为 numberOfRowsInSection 创建 if 语句?知道函数看起来像这样: - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { if (movies && movies.count) { return movies.count; } 其他 { 返回 0; } }
  • 是的,没关系,只需将该部分的行数设置为1,我编辑了答案
  • 应用程序现在崩溃。由于未捕获的异常“NSRangeException”而终止应用程序,原因:“*** -[__NSArrayM objectAtIndex:]: index 0 beyond bounds for empty array”
  • 好吧,我没有你的项目,如果没有……你知道它至少在哪一行崩溃吗?如果这会导致 [nib objectAtIndex...] 崩溃,那是因为您没有创建这些 nib,假设您为自定义单元格创建了一个 nib。
  • 你检查过我的编辑吗?我已经在那里发布了完整的 .m 代码。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-03-26
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多