2014-05-18 3 views
3

테이블보기의 첫 번째 행에 다른 스타일을 설정하려고합니다.테이블보기의 첫 번째 행에 대한 사용자 지정 스타일

나는 다음과 같은 코드로이 일을 해요 :

def tableView(table_view, cellForRowAtIndexPath: index_path) 
    data_row = @data[index_path.row] 
    puts index_path.row 
    if index_path.row == 0 
    cell = table_view.dequeueReusableCellWithIdentifier(ITEM_CELL_ID) || begin 
     rmq.create(ItemCell, :first_cell, reuse_identifier: ITEM_CELL_ID).get 
    end 
    else 
    cell = table_view.dequeueReusableCellWithIdentifier(ITEM_CELL_ID) || begin 
     rmq.create(ItemCell, :rest_cell, reuse_identifier: ITEM_CELL_ID).get 
    end 
    end 
    cell.update(data_row) 
    cell 
end 

질문

나는이 매우 이상한 행동을 얻을 그러나. 첫번째 열은 주문의 스타일을 가지고있다. ... 그러나 제 10의 그리고 제 20의 열은 그렇게한다!! 나는 그것이 왜 일어날 지 모른다. 행 2 - 9와 11 - 19는 행 0과 10 및 20과 다릅니다.

+0

무엇이'puts index_path.row'를 돌려 주는가? – dax

+0

그것은'0, 1, 2, 3 ... 10 ... 15 ... 20..' 등을 반환하고 있습니다. 나는 index_path.class.to_s를 넣고'Fixnum'을 반환했습니다. – birdy

+0

은 어둠에, 하지만 만약'if index_path.row/2 == 0'을 시도해보십시오 - 작동 할 수도 있지만 그렇지 않을 수도 있습니다 만, 그렇지 않다면 숫자 비교가 아니라는 것을 확실히 알게 될 것입니다 – dax

답변

0

는 각 세포 유형에 대해 서로 다른 ITEM_CELL_ID (reuseIdentifiers)를 사용합니다. 따라서 :first_cell 스타일 셀은 :rest_cell 스타일 셀보다 다른 reuseIdentifier 상수를 가져야합니다. 테이블을 스크롤 할 때 첫 번째 셀의 메모리가 반복적으로 재사용된다는 점을 고려하면 문제가 해결되어야합니다.

0

메서드가 호출되는 방법을 모르기 때문에 가능하지 않을 수 있습니다 ... 그러나 이것이 컬렉션 (또는 스택 어딘가에 관련된 컬렉션이 있음). 당신이 뭔가를 할 수 있습니다 :

#wherever you're calling this from 
index_paths.each_with_index do |path, i| #add an 'index path' 
    tableView(table_view, cellForRowAtIndexPath: path, i) 
    ... 
end 

#updated original method 
def tableView(table_view, cellForRowAtIndexPath: index_path, path_index) #add path index 
    data_row = @data[index_path.row] 
    puts index_path.row 
    if path_index == 0 #add path index 
    cell = table_view.dequeueReusableCellWithIdentifier(ITEM_CELL_ID) || begin 
     rmq.create(ItemCell, :first_cell, reuse_identifier: ITEM_CELL_ID).get 
    end 
    else 
    cell = table_view.dequeueReusableCellWithIdentifier(ITEM_CELL_ID) || begin 
     rmq.create(ItemCell, :rest_cell, reuse_identifier: ITEM_CELL_ID).get 
    end 
    end 
    cell.update(data_row) 
    cell 
end