繁体   English   中英

将 UIButton 放入 UITableView

[英]Put a UIButton inside a UITableView

我正在尝试将UIButton放在UITableViewCell内。

UITableView还具有包含UILabels的单元格,并且单元格中绘制了渐变背景。 我看到的问题是我的UIButton只是一个没有文本显示的填充黑色单元格,并且更改颜色和背景颜色属性似乎没有任何作用。

这是我用来创建按钮的代码:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSLog(@"InAppViewController, doing this - width= %f", self.wdth);
    static NSString *ProductTableIdentifier = @"ProductTableIdentifier";

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:ProductTableIdentifier];
    if (cell == nil)
    {
        if (self.wdth >= 700) {
            CGRect cellFrame = CGRectMake(0, 0, (self.wdth - 100), 40);
            cell = [[[UITableViewCell alloc] initWithFrame:cellFrame reuseIdentifier: ProductTableIdentifier] autorelease];

            // Set up some labels...

            CGRect seenValueRect = CGRectMake(self.wdth-320, 0, 100, 40);
            UIButton *seenValue = [UIButton buttonWithType:UIButtonTypeRoundedRect];
            [seenValue setBackgroundColor:[UIColor greenColor]];
            seenValue.tag = kSeenValueTag;
            seenValue.frame = seenValueRect;
            [seenValue setTitle:@"I'm a clone" forState:UIControlStateNormal];

            [cell.contentView addSubview:seenValue];

            [seenValue release];

            // Continue setting up cell...

pastebin 链接包含整个cellForRowAtIndexPath() function 的代码(对于有问题的UITableView )。

http://pastebin.com/bpEyfruE

也许问题在于您正在发布使用便捷初始化程序[UIButton buttonWithType:(UIButtonType)]初始化的 UIButton 。 尝试删除[button release]

它甚至不可能是你必须做[cell addSubview:seenValue]; 而不是[cell.contentView addSubview:seenValue];

您也可以尝试使用CGRectMake(110, 11, 185, 30)而不是CGRectMake(self.wdth-320, 0, 100, 40) :我使用它并且效果很好,但我不知道它是不是你的需要

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM