[英]UITable inside Scroll View or a Table View Header?
我目前正在構建一個應用,其中我將一些數據解析為UITableView
。 我正在使用自定義UITableViewCell
類。
我想在UITableView
之前和之上添加一個UIImage
,就像下面的UIImage
一樣。 我的問題是我是否應該UITableView
一個包含UIImage
的自定義UITableView
標頭視圖? 或者是否應該將UITableView
和UIImageView
放在UIScrollView
?
我該怎么辦?
當我滾動UITableView
我希望頂部的UIImage
消失,就像在Table View中是一行一樣。
我真的很感謝一些解決方案!
謝謝。
編輯:
這是完整的.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.