2012-12-31 49 views
0

在我的应用程序中,我有UITableView其中第一个单元格&最后三个单元格的内容是固定的。但是在这些单元格之间的内容是不同的,因为每个数组的内容为&。如何创建使用iPhone SDK的动态UITableView单元格

E.g:第一细胞+阵列+计数最后三个单元

数组数总是不一样。 如何解释上面的表格视图。

+0

有无你尝试一些事情?然后告诉我们... – Maulik

+0

不,我不知道该怎么做。 numberofrowsinsection方法中的 – user1673099

+0

返回为([array count] + 6)。并在cellForRowAtIndex:检查indexPath.row小于3或大于[arrayCount - 1],并为单独的索引路径行值写一个逻辑来显示你想要的。 –

回答

2

如果你想添加子视图持续三个细胞,然后你可以使用以下条件

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
static NSString *[email protected]"cellIdentifier"; 
UITableViewCell *cell=[tableView dequeueReusableCellWithIdentifier:cellIdentifier]; 

if (cell==nil) { 
    cell=[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier]; 
} 

if (indexPath.row==0) 
    [email protected]"First Cell"; 
else if (indexPath.row>[myArray count]) 
{ 
    int k=([indexPath row]-[myArray count]-1); 

    switch (k) { 
    case 0: 
    [cell.contentView addSubview:thirdLastView]; 
    break; 
    case 1: 
    [cell.contentView addSubview:secondLastView]; 
    break; 
    case 2: 
    [cell.contentView addSubview:lastView]; 
    break; 
    } 
    cell.textLabel.text=[lastFixedArray objectAtIndex:k]; 
} 
else 
    cell.textLabel.text=[myArray objectAtIndex:indexPath.row-1]; 

return cell; 
} 
+0

是的,伟大的编码! – user1673099

+0

再次感谢您的回复! – user1673099

1

在你numberOfRows方法,返回值作为

4 + [array count]; 

cellForRowAtIndexPathindexPath.row开关,并相应填写您的单元格内容。

3

//工作第一小区+ [动态数组] +最后三个固定单元代码

#import "tableTOViewController.h" 

@interface tableTOViewController() 

@end 

@implementation tableTOViewController 

- (void)viewDidLoad 
{ 
myArray=[[NSArray alloc] init]; 
myArray=[NSArray arrayWithObjects:@"Aman",@"Atul",@"Karan",@"Yen",@"Chan",@"Lee", nil]; 

lastFixedArray=[[NSArray alloc] init]; 
lastFixedArray=[NSArray arrayWithObjects:@"Snakes",@"Food",@"Drink", nil]; 

    tblView.delegate=self; 
    tblView.dataSource=self; 
    [super viewDidLoad]; 
} 

- (void)didReceiveMemoryWarning 
{ 
    [super didReceiveMemoryWarning]; 
    // Dispose of any resources that can be recreated. 
} 


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section 
{ 
return [myArray count]+4; 
} 

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
static NSString *[email protected]"cellIdentifier"; 
UITableViewCell *cell=[tableView dequeueReusableCellWithIdentifier:cellIdentifier]; 

if (cell==nil) { 
    cell=[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier]; 
} 

if (indexPath.row==0) 
    [email protected]"First Cell"; 
else if (indexPath.row>[myArray count]) 
    cell.textLabel.text=[lastFixedArray objectAtIndex:([indexPath row]-[myArray count]-1)]; 
else 
    cell.textLabel.text=[myArray objectAtIndex:indexPath.row-1]; 

return cell; 
} 


@end 
+0

代码为+1。以及为什么不尝试其他 - 如果条件而不是else语句中的if-else。 –

+0

我最近再次编辑代码检查 – Warewolf

+0

雅。我也编辑了评论。检查一下。 –

相关问题