表格视图中不同的单元格高度...iphone
我是 Iphone App 的新程序员......我想要表中的三个单元格(高度不同)
我为此任务编写代码......并且输出令人满意...... .但我不知道..这是正确的方法...
提前感谢...:)
CGRect frame=CGRectMake(5, 43, 311, 363);
UITableView *table=[[UITableView alloc]initWithFrame:frame style:UITableViewStylePlain];
table.dataSource=self;
table.delegate=self;
-(CGFloat) tableView:(UITableView *)tableView heightForRowAtIndexPath: (NSIndexPath *)indexPath
{
static int counter=1;
if(counter==1)
{
counter=counter +1;
return 163;
}
else if(counter ==2)
{
counter=counter +1;
return 80;
}
else
{
counter=counter +1;
return 60;
}
if(counter==4)
{
counter=1;
}
}
i am a new programmer of Iphone App..... i wants three cells in table (which are different height)
i write the code for this task..and output is satisfactory...... but i don't know.. this is right method or not.....
thanks in advance..... :)
CGRect frame=CGRectMake(5, 43, 311, 363);
UITableView *table=[[UITableView alloc]initWithFrame:frame style:UITableViewStylePlain];
table.dataSource=self;
table.delegate=self;
-(CGFloat) tableView:(UITableView *)tableView heightForRowAtIndexPath: (NSIndexPath *)indexPath
{
static int counter=1;
if(counter==1)
{
counter=counter +1;
return 163;
}
else if(counter ==2)
{
counter=counter +1;
return 80;
}
else
{
counter=counter +1;
return 60;
}
if(counter==4)
{
counter=1;
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
不,这是不对的。使用
indexPath.row
而不是counter
变量。这将是您的代表被询问的行号。例如,当向上滚动时,您当前的代码可能无法获得预期的结果。No, this isn't right. Use
indexPath.row
instead of yourcounter
variable. This will be the row number your delegate is being asked about. When scrolling upwards, for example, you may not get the expected results with your current code.不要使用
从 NSIndexPath 获取
行
。Don't use
Get the
row
from NSIndexPath.您的
static int counter
不是全局变量,因此您的代码无法工作。如果你用它制作一个伊瓦尔,它会起作用,但这不是解决这个问题的正确方法。
由
heightForRowAtIndexPath:
委托方法提供的indexPath
变量保存实际项目的索引。只需在代码中调用indexPath.row
(这是实际的计数器)而不是您的计数器。your
static int counter
is not a global variable, so you code can't work.It would work, if you make an ivar from it but that is not the correct way to solve that.
The
indexPath
variable provided by theheightForRowAtIndexPath:
delegate method holds the index of the actual item. Just callindexPath.row
(this is the actual counter) in your code instead of your counter.从你的代码来看,我猜你有 3 行高度的旋转,所以最好的方法是使用
int row = indexPath.row % 3;
这会给你一个循环 0,1,2 作为值代替你的计数器进行测试From your code I guess you have a rotation of 3 rows heights, so the best way is to use
int row = indexPath.row % 3;
this will give you a cycling 0,1,2 as value to test instead of your counter