2014-02-13 79 views
0

我需要添加按鈕來分離細胞在黃圈添加按鈕,獨立的細胞

按鈕

Button in yellow circle

我可以添加到像[cell addSubview:myButt];所有細胞,但它工作於所有細胞,我需要只有5,6,7,8個細胞;

我是否需要編寫自定義UITableViewCell

那條線(黑色箭頭),據我瞭解它是Section,我怎樣才能創建它沒有標題,線(圖像)和表中間?謝謝大家。

P.S.對不起,我的英語

+1

寫一些檢查一樣,如果(indexPath.section == 1)&如果(indexPath.row == 0){[細胞addSubview: myButt]; } else if(indexPath.row == 1){[cell addSubview:myButt];}等等。它真的很容易。爲自己而努力。祝你好運 – Pawan

+1

你可以在'cellForRowAtIndexPath:'中使用'viewWithTag:'做到這一點,但它很快就會變得煩人。你*應該*子類UITableViewCell:視圖邏輯屬於一個UIView子類。 –

回答

-1

就在cellforrow方法中添加按鈕:

If (indexpath.row == 3 || indexpath.row == 4 || indexpath.row == 5 || indexpath.row == 3) 
{ 
    // add button here 
    // tag button here 
    Button.tag = indexpath.row ; 
} 
+0

然後當用戶上下滾動時,該按鈕將被重用。 –

+0

它會被重用,但它也會始終可見,即使它不是正確的單元格。 – jbouaziz

+0

這不是最好的方法,但它的工作,謝謝。 –

4

可以隱藏你的節標題是這樣的:

- (CGFloat) tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section 
{ 
    return 0.0f; 
    // or 1.0 as per your line 
} 

有關你的細胞按鈕,你必須創建一個自定義cell Class,然後在cellForRowAtIndexPath方法中使用它。你也可以爲此使用標籤。你可以這樣說:

- (UITableViewCell *)tableView:(UITableView *)theTableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
UITableViewCell *cell = [theTableView dequeueReusableCellWithIdentifier:"Cell"]; 
if (cell == nil) { 
    cell = [[CustomCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"Cell"]; 
} 
if(indexPath.section == 1) 
{ 
    if(indexPath.row>=5 && indexPath.row <=8) 
    { 
    //adding buttons to custom cell 
    } 
} 
} 
+0

你問了關於截面線。它有用嗎? –

1

你可以使用的UIViewtag屬性來跟蹤你的按鈕,但我真的建議使用自定義UITableViewCell。這就是說,下面是你可以使用的代碼。它被優化爲不需要一遍又一遍地添加相同的按鈕,但只是在你需要它的時候,並隱藏在你不需要的情況下。

static int kButtonViewTag = 3294802; 
UIButton *button = [cell.contentView viewWithTag:kButtonViewTag]; 
BOOL shouldDisplayButton = indexpath.row == 5 || indexpath.row == 6 || indexpath.row == 7 || indexpath.row == 8; 

// If the button should be displayed and is not 
// 
if (shouldDisplayButton) { 

    // Add button here 
    // 
    if (!button) { 
     button = // Init your button 
     button.tag = kButtonViewTag; 
     [cell.contentView addSubview:button] 
    } 
    else if (button.isHidden) { 
     button.hidden = NO; 
    } 
} 


// If the button should not be displayed but is 
// 
else if (!shouldDisplayButton && button && !button.isHidden) { 
    button.hidden = NO; 
} 

然後爲部分:

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section { 
    CGFloat tableWidth = tableView.frame.size.width; 
    CGFloat padding = 10; // Add some padding 

    UIView *view = [[UIView alloc] initWithFrame:CGRectMake(padding, 0, tableWidth - padding * 2, 1)]; 
    [view setBackgroundColor:[UIColor greyColor]; //your background color... 
    return view; 
}