而不是膨脹和卷積這個職位與我已經嘗試和失敗,我只是保持簡單,因爲我敢肯定,答案是比我想大概簡單。如何以編程方式使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的一個子類。我想我明白你在說什麼。現在處理它。感謝兩位回答。
非常感謝,伊斯梅爾!這正是我需要的。真棒。 – baptzmoffire
雖然做了一些小修改。在你的例子的第9行中,'tableRect'應該是上面定義的'bounds'。 self.tableView = [[UITableView alloc] initWithFrame:bounds style:UITableViewStyleGrouped]; //或簡單的,無論你需要什麼 – baptzmoffire
你也需要從協議UITableViewDataSource實現兩個@required方法 - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section; AND - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath; – horkavlna