![](/img/trans.png)
[英]Is a UITableViewCell and its outlets initialized after `dequeueReusableCellWithIdentifier`?
[英]Subviews of UITableViewCell seem to randomly disappear after dequeueReusableCellWithIdentifier
在我的iOS 7.0應用中:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
...
// AttemptCell is a prototype cell, currently using the "Right Detail" preset
// style and the little information accessory.
static NSString *CellIdentifier = @"AttemptCell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
assert(cell != Nil);
if (cell.contentView.subviews.count == 2)
{
UILabel *attemptLabel = (UILabel*)cell.contentView.subviews[0];
attemptLabel.text = attempt.attempt;
UILabel *analysisLabel = (UILabel*)cell.contentView.subviews[1];
analysisLabel.text = [attempt analysis];
cell.tag = indexPath.row;
}
else
{
// Something has gone very wrong.
UILabel *attemptLabel = (UILabel*)cell.contentView.subviews[0];
attemptLabel.text = @"Error";
}
問題是,為什么(UILabel *)cell.contentView.subviews [1]有時會消失,導致輸入錯誤塊。
此表視圖顯示一個自定義鍵盤輸入單元格(UITextField),該單元格始終顯示在最后。 鍵盤輸入單元也已原型化,但具有不同的出隊單元標識符。 當鍵盤彈出並關閉時,會隨機出現此問題。 彈出鍵盤會導致某些AttemptCells消失,而關閉鍵盤會使AttemptCells重新回到視野。
你在做什么錯了。 不要依賴於私有類的視圖層次結構,當然不依賴於層次結構中視圖的數量,並且實際上也不依賴於位於子視圖數組中某個位置的視圖。 可能由於子視圖“消失”而無法輸入錯誤塊-可能已經添加了一個額外的視圖,您所要檢查的只是子視圖的數量等於2。
如果使用的是標准單元textLabel
detailTextLabel
,請使用textLabel
和detailTextLabel
屬性。 如果您使用的是子類,請使用插座。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.