设置里面 tableviewcell cell.textlabel的位置 的位置吗

[ios]UITableView: 处理混合单元格表格视图静态和动态单元格中的单元格选择
注意事项: 本文中文内容可能为机器翻译,如要查看英文原文请点击上面连接.
我想混在分组的表视图中的动态和静态细胞: 我想要两个部分与静态细胞在遵循的一条动态的单元格的顶部 (请参阅下面的屏幕快照)。我已将表视图内容设置为静态的单元格。
基于 AppleFreak 的忠告我改变了我的代码,如下所示:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
static NSString *CellIdentifier = @"Cell";
UITableViewCell *
if (indexPath.section &= 1) { // section &= 1 indicates static cells
cell = [super tableView:tableView cellForRowAtIndexPath:indexPath];
} else { // section & 1 indicates dynamic cells
CellIdentifier = [NSString stringWithFormat:@"section%idynamic",indexPath.section];
cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
然而,我的应用程序崩溃的错误消息
终止未捕获的异常 'NSInternalInconsistencyException' 的应用程序,原因: ' UITableView 数据源必须从 tableView:cellForRowAtIndexPath 返回单元格: '
科 0 和 0 的行。从返回的单元格 cell = [super tableView:tableView cellForRowAtIndexPath:indexPath] 科 0 和行 0 是 nil 。
我的代码有什么毛病?可能会有任何问题与我的零售商吗?我没设置任何零售商,因为我创建子类 UITableViewController 和假想做没有任何零售商连接设置 (?)。任何的建议如何更好地做到这一点吗?
我已重新创建我的现场演示图板中 (请参阅以上我已更新截图) 和重写视图控制器开始从一个新的基地。我还读了苹果的论坛中的描述,作为 applefreak 建议。然而,我在我第一个问题与方法运行 numberOfSectionsInTableView:tableView ,我由一个递增的静态部分 (两个) 数。
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return [super numberOfSectionsInTableView:tableView] + 1 ; }
App 坠毁并显示错误消息:
终止未捕获的异常 'NSRangeException' 的应用程序,原因: '* -[__NSArrayI objectAtIndex:]: 索引超出界限 2 [0...1]'
为什么这段代码不为我工作即使我跟着苹果的和 applefreak 的建议?它是可能连接已有点 iOS 6 中改变了吗?
解决方案: 搞工作现在在他下面的回答中使用 AppleFreaks 的代码示例。谢谢你,AppleFreak!
编辑第三: 单元格选定内容:
如何面对混合 (动态和静态细胞) 的单元格的表视图中的单元格选择?当我打电话 super 时不要打电话给 self tableView ?何时使用
[[super tableView] selectRowAtIndexPath:indexPath animated:NO scrollPosition:UITableViewScrollPositionNone]
并尝试检查与选定的索引路径:
UITableView *tableView = [super tableView];
if ( [[tableView indexPathForSelectedRow] isEqual:customGrowthIndexPath] ) { .. }
我得到的返回值 nil 。
因为我找不到我的错误的来源,我真的很感谢你的帮助
解决方法 1:
对于静态的单元格,您需要调用超级方法。我相信你将会扩大 UITableViewController。请参阅下面的
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
static NSString *CellIdentifier = @"Cell";
UITableViewCell *
/* Detect cell is static or dynamic(prototype) based on the index path and your settings */
if ("Cell is prototype")
cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
else if ("Cell is static")
cell = [super tableView:tableView cellForRowAtIndexPath:indexPath];
// Modify cell properties if you want
此外有关混合单元格的详细信息请参阅混合静态和动态表视图内容讨论苹果论坛上。
如果你没有读过苹果链接上面那么请这样做仔细。为动态内容您将生成单元格第一次与给定的标识符代码本身中。在病房上的下一次你将取消排队,该单元格,而不是建设它 !这是同样的老方法。
此外,请记住静态数据源认为仅有 2 节 (例如)。你不能问它 2 节,因为它认为有 0 和 1 的唯一部分。如果你要将插入任意位置的动态内容但表的末尾,您需要用谎言来超级当您转发的数据源的方法。您的 numberOfRowsInSection: 方法,例如,应该看看这样的事情:
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
if (section == 1) {
else if (section & 1){
section--; // Basically you are forming sections for dynamic content
return [super tableView:tableView numberOfRowsInSection:section];
关键要为您的动态内容的调整,以便静态数据源仍获取它期望的值
这里是完整的工作示例并进行测试。只是复制过去的所有在您的代码中的以下方法,它应该工作直接方式。你必须重写所有的连接方法在您具有混合的单元格。返回的总数的静态和动态的各节中根据您的要求,"numberOfSectionsInTableView",然后在每个其余的方法 ;如果节或行问题是静态只是调用通过超,如果它是动态传递回有关的细节,你会在正常的 UITableViewController 子类中。在此示例中,我只回零或硬编码的值。
详细信息请查看此线程苹果论坛上。
- (int)numberOfSectionsInTableView:(UITableView *)tableView
- (NSString*)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
- (NSString*)tableView:(UITableView *)tableView titleForFooterInSection:(NSInteger)section
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath
return NO;
- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath
return NO;
-(float)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
return 44.0f;
- (float)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
return 44.0f;
- (float)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
return 44.0f;
- (UIView*)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section
-(UIView*)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
-(int)tableView:(UITableView *)tableView indentationLevelForRowAtIndexPath:(NSIndexPath *)indexPath
- (int)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
if(section == 0)
if(section == 1)
- (UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
if(indexPath.section &= 1)
return [super tableView:tableView cellForRowAtIndexPath:indexPath];
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil)
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
cell.textLabel.text = @"dynamic row";
你没有叫超级 didSelectRowAtIndexPath。它是直向前。请参见下面的代码。
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
int row = indexPath.
[tableView deselectRowAtIndexPath:indexPath animated:YES];
UITableViewCell *selectedCell = [tableView cellForRowAtIndexPath:indexPath];
//process the cell主题 : 【新人求助】怎样用代码设置cell中textlabel位置和大小
级别: 新手上路
可可豆: 155 CB
威望: 145 点
在线时间: 44(时)
发自: Web Page
来源于&&分类
【新人求助】怎样用代码设置cell中textlabel位置和大小&&&
新人一枚,若问题太SB请海涵。想用代码定义一下cell中label和detailTextLabel的位置和大小cell.textLabel.Frame = CGRectMake(0, 0, 0, 0);这个貌似直接没有效果...求教.....
级别: 侠客
可可豆: 264 CB
威望: 173 点
在线时间: 321(时)
发自: Web Page
回 楼主(黑色魔术师) 的帖子
Cell是有很多Style的如果想自定义,请先在Cell的面板里,将Style设置为custom然后就可以改想改的东西了,如果想改的东西很多的话,可以自己继承一个UITableVIewCell的子类然后将Cell上的控件拖拽为属性
努力学吧,不会的东西会越来越多的。
级别: 新手上路
可可豆: 155 CB
威望: 145 点
在线时间: 44(时)
发自: Web Page
回 1楼(bifidy) 的帖子
哦...我先研究一下怎么设置~因为这一段我是跟着教程做的,是纯代码,我又是新手,所以不能很快能理解其中的内容...
级别: 侠客
可可豆: 264 CB
威望: 173 点
在线时间: 321(时)
发自: Web Page
回 2楼(黑色魔术师) 的帖子
额,刚开始就学着用代码写TableView,一定会死人的= =个人感觉还是从UI开始定性了解,再逐渐通过代码控制去定量,最后能做到UI与代码之间可以相互转换,互为辅助,比较好毕竟用代码写UI是个挺蛋疼的事情= =
努力学吧,不会的东西会越来越多的。
级别: 新手上路
可可豆: 328 CB
威望: 328 点
在线时间: 26(时)
发自: Web Page
级别: 新手上路
可可豆: 196 CB
威望: 158 点
在线时间: 85(时)
发自: Web Page
cell.textLabel.Frame = CGRectMake(0, 0, 0, 0);长宽都为0怎么看得出有效果还是没效果喜欢跟着例子做的话推荐《iOS Development Recipes》,全是例子,网上有 PDF
级别: 新手上路
UID: 307754
可可豆: 205 CB
威望: 110 点
在线时间: 27(时)
发自: Web Page
如果你用系统cell的 detailtext&&貌似是不能设置位置的,他就是固定的位置。你得自己用一个&&继承于&&UITableViewCell 的类,来写自己的cell然后在 那个
-(UITableViewCell *)table………………&& (UITableView 的delegate这个方法中){static NSString *…………;UITableViewCell *cell = ………………;&&//这里的 UITableViewCell&&换成你自己写的cellif (nil == cell)cell = [UITableViewCell alloc]init ………………; //这里的 UITableViewCell&&也换成你自己写的cell//后面不变}你自定制之后,自己设置detailtext
正在努力中的程序员……
级别: 新手上路
可可豆: 328 CB
威望: 328 点
在线时间: 26(时)
发自: Web Page
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath里边手写一个label &&&&UILabel *label = [[UILabel alloc]initWithFrame:CGRectMake(0, 5, 100, 30)];&&&&label.autoresizingMask = UIViewAutoresizingFlexibleW&&&&[label setText:XXXXXX];&&&&label.font = [UIFont systemFontOfSize:17];&&&&label.textColor = [UIColor blueColor];&&&&label.textAlignment = NSTextAlignmentC&&&&label.backgroundColor = [UIColor clearColor];&&&&&&&&[cell.contentView addSubview:label];或者自己写在cell的子类里我也是新手。。。希望有用。
关注本帖(如果有新回复会站内信通知您)
个人IDP证书一年费用? 正确答案:99美金
发帖、回帖都会得到可观的积分奖励。
按"Ctrl+Enter"直接提交
关注CocoaChina
关注微信 每日推荐
扫一扫 浏览移动版}

我要回帖

更多关于 tableviewcell label 的文章

更多推荐

版权声明:文章内容来源于网络,版权归原作者所有,如有侵权请点击这里与我们联系,我们将及时删除。

点击添加站长微信