代码创建控件
UITableView *myTableView = [[UITableView alloc] initWithFrame:CGRectMake:(0,0,320,480)
style:UITableViewStylePlain];
myTableView.delegate = self;
myTableView.dataSource = self;
[self.view addSubView:myTableView];
//myArr 为一维数组
Methods:
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return [myArr count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *identifier = @"identifier";
UITableViewCell *cell = [tableView dequeueReusableCellWithI
dentifier:identifier];
if (cell == nil)
{
//有四种格式可以选择
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValu
e1
reuseIdentifier:identifier]autorelease];
}
NSInteger row = [indexPath row];
cell.textLabel.text = [myArr objectAtIndex:row];
cell.textLabel.font = [UIFont boldSystemFontOfSize:16];
//后面要添加的内容从文件中读取
if (row == 0) {
cell.detailTextLabel.text = @"detail info"
;
}
return cell;
}
//设定每个cell的高度
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
return 70;
}
//设定Header的标题内容,不论tableView是plain还是group都有header
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{
return @"详细内容";
}
//设定tableView与上边界的距离
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection
:(NSInteger)section
{
return 44;
}
//在每个section上方创建一个view,可放任何控件,注意section个数
//plain和group的区别:样式为group时headerView跟随tableView一起上升,plain时滑动到底部时才会上升,底部的时候没试验...
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
UIView* customView = [[UIView alloc] initWithFrame:CGRectMake(10.0, 0.0, 300.0, 44.0)];
UILabel * headerLabel = [[UILabel alloc] initWithFrame:CGRectZero];
headerLabel.backgroundColor = [UIColor clearColor];
headerLabel.opaque = NO;
headerLabel.textColor = [UIColor blackColor];
headerLabel.highlightedTextColor = [UIColor whiteColor];
headerLabel.font = [UIFont boldSystemFontOfSize:16];
//转屏幕的时候设置标题位置,如不需要转屏,可直接设置frame
UIInterfaceOrientation orientation = [UIApplication sharedApplication].statusBarOrientation;
if (orientation == UIInterfaceOrientationPo
rtraitUpsideDown || orientation == UIInterfaceOrientationPo
rtrait)
{
headerLabel.frame = CGRectMake(10.0, 0.0, 300.0, 44.0);
}
else {
headerLabel.frame = CGRectMake(30.0, -10.0, 300.0, 44.0);
}
headerLabel.text = @"标题";
[customView addSubview:headerLabel];
return customView;
}
//二维数组创建与显示,多个section,cell个数不同
array = [[NSArray arrayWithObjects:
[NSArray arrayWithObjects:@"aaa:",@"bbb:", nil],
[NSArray arrayWithObjects:@"ccc:", nil],
[NSArray arrayWithObjects:@"ddd", nil],
nil] retain];
- (NSInteger)numberOfSectionsInTableV
iew:(UITableView *)tableView
{
// Return the number of sections.
return [singleDatas count];
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
NSArray* array1 = (NSArray*)[singleDatas objectAtIndex:section];
assert(array1 != nil);
return [array1 count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *identifier = @"identifier";
UITableViewCell *cell = [tableView dequeueReusableCellWithI
dentifier:identifier];
if (cell == NULL)
{
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValu
e1 reuseIdentifier:identifier] autorelease];
int row = [indexPath row];
int section = [indexPath section];
NSArray* array1 = (NSArray*)[singleDatas objectAtIndex:section];
assert(array1 != nil);
NSString* str = (NSString*)[array1 objectAtIndex:row];
//NSLog(@"%@", str);
cell.textLabel.text = str;
cell.textLabel.font = [UIFont fontWithName:@"Arial" size:13.0];
}
return cell;
}
开发过程中遇见的问题:
代码创建的UITableView,添加内容后滑动不到底部,或者看不到底部的一些行,解决方法为添加一行代码,在IB中也有此属性的配置:
[myTableView setContentInset:UIEdgeInsetsMake(0, 0, 100, 0)];
这是我自己试验出的方法,我自己程序中可行,也不知道其他人都怎么设置的。有其他问题再补充。
//补充
有时我们需要动态调整UITableViewCell的高度,根据内容的不同设置不同的高度,以前看到一种实现方法,写得有点麻烦,具体地址找不到了,这里有个更好的(至少我认为),分享一下部分代码。
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier:CellIdentifier] autorelease];
UILabel *label = [[UILabel alloc] initWithFrame:CGRectZero];
label.tag = 1;
label.lineBreakMode = UILineBreakModeWordWrap;
label.highlightedTextColor = [UIColor whiteColor];
label.numberOfLines = 0;
label.opaque = NO; // 选中Opaque表示视图后面的任何内容都不应该绘制
label.backgroundColor = [UIColor clearColor];
[cell.contentView addSubview:label];
[label release];
}
UILabel *label = (UILabel *)[cell viewWithTag:1];
NSString *text;
text = [textArray objectAtIndex:indexPath.row];
CGRect cellFrame = [cell frame];
cellFrame.origin = CGPointMake(0, 0);
label.text = text;
CGRect rect = CGRectInset(cellFrame, 2, 2);
label.frame = rect;
[label sizeToFit];
if (label.frame.size.height > 46) {
cellFrame.size.height = 50 + label.frame.size.height - 46;
}
else {
cellFrame.size.height = 50;
}
[cell setFrame:cellFrame];
return cell;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell = [self tableView:tableView cellForRowAtIndexPath:indexPath];
return cell.frame.size.height;
}
经验:cell最好用自定义的,可以按照你想要的结果来设计,效果很好,如图:
转自:http://www.cnblogs.com/batys/archive/2011/10/18/2216434.html