簡體   English   中英

在UITableView for iPhone SDK中更改備用行的顏色

[英]Change color of alternate row in UITableView for iPhone SDK

在我的應用程序中,我想在iPad中顯示多列tableView。

所以,我使用了來自MultiColumn TableView的cocoa控件

它適用於我,但我想以交替顏色顯示行。

為此,我無法找到我為其更改代碼的位置。

幫我解決這個問題。

請嘗試使用它,它會工作正常..

- (void)tableView: (UITableView*)tableView willDisplayCell: (UITableViewCell*)cell forRowAtIndexPath: (NSIndexPath*)indexPath
{

        if(indexPath.row % 2 == 0)
              cell.backgroundColor = [UIColor redColor];
        else
              cell.backgroundColor = [UIColor whiteColor];
}

使用cellForRowAtIndexPath的indexPath來獲得所需的結果。

    if( [indexPath row] % 2){
          cell.backgroundColor=[UIColor whiteColor];
    } 
    else{
          cell.backgroundColor=[UIColor purpleColor];
    }

您將在實現UITableViewDelegate的類中使用此委托方法

試試這個代碼::

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    .......

    if (indexPath.row % 2 == 0) {
       cell.backgroundColor = [UIColor lightGrayColor];
    }
    else
    {
        cell.backgroundColor = [UIColor darkGrayColor];
    }
    .......

    return cell;
}

希望,它會幫助你。

謝謝。

首先,您需要對每行進行模塊化。

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    .......

    if (indexPath.row % 2 == 0) {
       cell.backgroundColor = [UIColor lightGrayColor];
    }
    else
    {
        cell.backgroundColor = [UIColor darkGrayColor];
    }
    ....


    return cell;
}

任何試圖在swift中執行此操作的人都是我的代碼。 小巧精湛。

這個技巧甚至適用於我不使用cellForRowAtIndexPath方法的靜態單元格。

對不起,小答案。 希望您了解一個代表的數據源。

 override func tableView(tableView: UITableView, willDisplayCell cell: UITableViewCell, forRowAtIndexPath indexPath: NSIndexPath) {

    if (indexPath.row % 2) != 0{
        cell.backgroundColor = UIColor .blackColor()
    }else{
        cell.backgroundColor = UIColor .lightGrayColor()

    }

 }

它會給你帶來像...的結果

使用Swift為ios中的每個備用單元着色

謝謝

希望這有幫助。

嘗試這樣的

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {


static NSString *CellIdentifier = @"Cell";

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
    cell = [[[UITableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier:CellIdentifier] autorelease];
}
    if(indexPath.row%2==0)
        cell.contentView. backgroundColor=[UIColor redColor];
    else
        cell.contentView.backgroundColor=[UIColor greenColor];




return cell;


}

在代碼中搜索cellForRowAtIndexPath。 該方法負責在tableview中創建單元格。 有關更多信息: UITableViewCell在自定義單元格中具有備用背景顏色

在你的cellForRow方法中你應該檢查indexPath.row是否可以被2分配第一個顏色,否則分配第二個顏色並返回結果單元格

func colorForIndex(index: Int) -> UIColor 
{
    let itemCount = stnRepos.count - 1
    let color = (CGFloat(index) / CGFloat(itemCount)) * 0.6
    return UIColor(red: 0.80, green: color, blue: 0.0, alpha: 1.0)
}

func tableView(tableView: UITableView, willDisplayCell cell: UITableViewCell,
    forRowAtIndexPath indexPath: NSIndexPath) 
{        
    if (indexPath.row % 2 == 0)
    {
        cell.backgroundColor = colorForIndex(indexPath.row)
    } else {
        cell.backgroundColor = UIColor.whiteColor()()
    }
}

對於Swift 3 +並且不透明度降低:

func tableView(_ tableView: UITableView, willDisplay cell: UITableViewCell, forRowAt indexPath: IndexPath) {

        if(indexPath.row % 2 == 0) {
            cell.backgroundColor = UIColor.red.withAlphaComponent(0.05)
        } else {
            cell.backgroundColor = UIColor.white
        }
}

在這樣一個簡單的例子中,我會選擇三元運算符。 我只是討厭看到cell.backgroundColor重復。

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {

.
.
.
    cell.backgroundColor = indexPath.row % 2 == 0 ? UIColor.red.withAlphaComponent(0.05) : .white
.
.
.
}

你可以運行一個循環,它將索引path.row增加2,然后在該循​​環的主體中你可以分配顏色。

這是Swift 4.x&Above的工作代碼

func tableView(_ tableView: UITableView, willDisplay cell: UITableViewCell, forRowAt indexPath: IndexPath) {
    if(indexPath.row % 2 == 0) {
        cell.backgroundColor = UIColor.white
    } else {
        cell.backgroundColor =  UIColor.lightGray
    }
}

快樂編碼:)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM