2012-11-26 21 views
0

而不是膨脹和卷積這個職位與我已經嘗試和失敗,我只是保持簡單,因爲我敢肯定,答案是比我想大概簡單。如何以編程方式使UITableView更小?

我有我的應用程序的主視圖中滾動的UITableView。所有我想要做的是移動的默認位置 - 或「起點」 - 滾動的UITableView的下跌約194點,以騰出空間給我的導航欄和一些其他UI元素。我該怎麼做呢?下面是我所相信的是分別來自我的視圖控制器的.h和.m文件,相關的方法實現:

//  MGViewController.h 
//  UITVPractice 

#import <Foundation/Foundation.h> 

@interface ItemsViewController : UITableViewController 

-(id) init; 
-(id) initWithStyle:(UITableViewStyle)style; 

@end 



//  MGViewController.m 
//  UITVPractice 

#import "ItemsViewController.h" 
#import "MGItem.h" 
#import "MGItemStore.h" 

@implementation ItemsViewController 

-(void)viewDidLoad { 
    UIImageView *backgroundImageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"background.png"]]; 
    self.tableView.backgroundView = backgroundImageView; 

    [super viewDidLoad]; 
} 

-(id) init { 
    self = [super initWithStyle:UITableViewStyleGrouped]; 

    if (self) { 
     /* create 5 random MGItems and place in the MGItemStore */ 
     for (int i = 0; i < 20; i++) { 
      [[MGItemStore sharedStore] createItem]; 
     } 
    } 
    return self; 
} 

-(NSInteger)tableView:(UITableView *)tableView 
numberOfRowsInSection:(NSInteger)section 
{ 
    return [[[MGItemStore sharedStore] allItems] count]; 
} 

-(UITableViewCell *)tableView:(UITableView *)tableView 
     cellForRowAtIndexPath:(NSIndexPath *)indexPath { 

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"UITableViewCell"]; 

    /* Create an instance of UITableViewCell */ 
    if (!cell) { 
    cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault 
                reuseIdentifier:@"UITableViewCell"]; 
    } 

    UIView *backView = [[UIView alloc] initWithFrame:CGRectZero]; 
    backView.backgroundColor = [UIColor clearColor]; 
    cell.backgroundView = backView; 

    /* Display custom background image for cell(s) */ 
    cell.backgroundView = [[UIImageView alloc] initWithImage:[[UIImage imageNamed:@"cellBackground.png"] stretchableImageWithLeftCapWidth:0.0 topCapHeight:5.0]]; 

    /* Display custom background image for selected cell(s) */ 
    cell.selectedBackgroundView = [[UIImageView alloc] initWithImage:[[UIImage imageNamed:@"cellBackgroundTouched.png"] stretchableImageWithLeftCapWidth:0.0 topCapHeight:5.0]]; 

    /* eliminate the white box that bounds the black text. */ 
    [[cell contentView] setBackgroundColor:[UIColor clearColor]]; 
    [[cell backgroundView] setBackgroundColor:[UIColor clearColor]]; 
    [cell setBackgroundColor:[UIColor clearColor]]; 

    /* Set the text of the cell to the description of the item that is at the nth index of items, where n = row this cell will appear in on the tableView */ 
    MGItem *p = [[[MGItemStore sharedStore] allItems] objectAtIndex:[indexPath row]]; 
    [[cell textLabel] setText:[p description]]; 
    [[cell textLabel] setTextColor:[UIColor whiteColor]]; 
// [[cell textLabel] highlightedTextColor: [UIColor purpleColor]]; 

    return cell; 
} 

-(id) initWithStyle:(UITableViewStyle)style { 
    return [self init]; 
} 

@end 

我很抱歉,如果這個職位脫落爲「爲我做這個」的帖子,但我我已經嘗試了十幾種不同的東西,但都沒有成功。一直持續了約3天。感謝您的任何幫助,您可以提供。

編輯:是的,伊斯梅爾,你是正確的。它是UITableViewController的一個子類。我想我明白你在說什麼。現在處理它。感謝兩位回答。

回答

1

我把它的控制器是UITableViewController一個子類?如果是這樣的話,那麼你將不得不改變這一點。

爲了做到這一點,將子類更改爲UIViewController,添加UITableViewDelegateUITableViewDataSource協議。

然後,添加一個UITableView *的tableView屬性,並更改您的viewDidLoad方法看起來像這樣:

-(void)viewDidLoad { 
    [super viewDidLoad]; // [super viewDidLoad]; should go first!! 

    CGFloat startingPoint = 194.0; // or whatever 
    CGRect tableRect = self.view.bounds; 
    tableRect.origin.y = startingPoint; 
    tableRect.size.height -= startingPoint; 

    self.tableView = [[UITableView alloc] initWithFrame:tableRect style:UITableViewStyleGrouped]; // or plain, whichever you need 
    self.tableView.dataSource = self; 
    self.tableView.delegate = self; 
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleSingleLine; // do this if grouped, looks better! 
    self.tableView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight; 
    [self.view addSubview:self.tableView]; 

    UIImageView *backgroundImageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"background.png"]]; 
    self.tableView.backgroundView = backgroundImageView; 

} 

編輯:有一個原因。在正常的UIViewController中,self.viewUIView,但是在UITableViewController中,self.viewself.tableView相同,並且是UITableView,這就是爲什麼您無法更改它的框架。

+0

非常感謝,伊斯梅爾!這正是我需要的。真棒。 – baptzmoffire

+0

雖然做了一些小修改。在你的例子的第9行中,'tableRect'應該是上面定義的'bounds'。 self.tableView = [[UITableView alloc] initWithFrame:bounds style:UITableViewStyleGrouped]; //或簡單的,無論你需要什麼 – baptzmoffire

+0

你也需要從協議UITableViewDataSource實現兩個@required方法 - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section; AND - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath; – horkavlna

2

做到這一點,最好的辦法就是「鳥巢」另一個的UIView裏面的tableview控制器。如果您正在使用故事板,只需添加一個「容器視圖」,然後將該視圖控制器的類設置爲您的tableview控制器的類。然後,更改容器視圖的大小將自動更改表視圖的大小。這裏有一個例子,我有嵌套在一個視圖中的幾個表視圖:

enter image description here

相關問題